What I want to achieve, add providersconditionally, 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 classto implementit:
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 modulefor 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 StorageServiceto 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 StorageServiceinto 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 IonicStorageModuleto the main NgModulething that is missing here?