What I want to achieve, add providers
conditionally, as follows:
did interface
:
export interface StorageInterface {
Storage(): any;
SwitchStorage(isLocal: boolean): void;
ClearStorage(): void;
Setter(key: string, value: any): void;
Getter(key: string): any;
}
then did a class
to implement
it:
export class IonicStorage implements StorageInterface {
get User(): User {
return {};
}
set User(value: User) {
}
constructor( @Inject(forwardRef(() => Storage)) private storage: Storage) {
}
Storage() {
return this.storage;
}
SwitchStorage(isLocal: boolean): void {
}
ClearStorage(): void {
this.storage.clear();
}
Setter(key: string, value: any): void {
}
Getter(key: string) {
}
}
then a module
for processing suppliers, etc.
@NgModule({})
export class APIModule {
static forRoot(config?: Configuration) {
let mod = {
ngModule: APIModule,
imports: [
],
providers: [
{ provide: 'StorageInterface', useClass: IonicStorage },
StorageService,
AuthService,
]
};
return mod;
}
}
and I made StorageService
to play as middle man
/ proxy
:
@Injectable()
export class StorageService implements StorageInterface {
constructor( @Inject(forwardRef(() => 'StorageInterface')) private storage: StorageInterface) {
}
Storage() {
return this.storage;
}
SwitchStorage(isLocal: boolean): void {
this.storage.SwitchStorage(isLocal);
}
ClearStorage(): void {
this.storage.ClearStorage();
}
Setter(key: string, value: any): void {
this.storage.Setter(key, value);
}
Getter(key: string) {
return this.storage.Getter(key);
}
}
so I can insert StorageService
into any page/component
, etc., and it automatically uses the class I installed in the module usesClass
.
eg:
export class HomePage {
constructor(public navCtrl: NavController, public navParams: NavParams, private http: Http, private storage: StorageService) {
}
}
but he is mistaken:
Uncaught (in promise): Error: No provider for Storage! Error: No provider for Storage! at injectionError (http:
where I added IonicStorageModule
to the main NgModule
thing that is missing here?