Hello,
I am fairly new to Ionic 2. I like what I see so far but I have been having a problem injecting the Storage object into a service that I wrote.
Here is an excerpt from the Ripple log in the Developer tools of Chrome:
Unhandled Promise rejection: Error in ./MyApp class MyApp - inline template:0:0 caused by: No provider for Storage! ; Zone: ; Task: Promise.then ; Value: ViewWrappedError_nativeError: Error: Error in ./MyApp class MyApp - inline template:0:0 caused by: No provider for Storage!
I get the same thing when look at the logcat from adb I also get a similar error:
I/MultiDex(29845): VM has multidex support, MultiDex support library is disabled.
I/chromium(29041): [INFO:CONSOLE(48452)] “EXCEPTION: Error in ./MyApp class MyApp - inline template:0:0 caused by: No provider for Storage!”, source: file:///android_asset/www/build/main.js (48452)
I/chromium(29041): [INFO:CONSOLE(48454)] “ORIGINAL EXCEPTION: No provider for Storage!”, source: file:///android_asset/www/build/main.js (48454)
I/chromium(29041): [INFO:CONSOLE(48457)] “ORIGINAL STACKTRACE:”, source: file:///android_asset/www/build/main.js (48457)
I/chromium(29041): [INFO:CONSOLE(48458)] "Error: No provider for Storage!
I/chromium(29041): at NoProviderError.BaseError [as constructor] (file:///android_asset/www/build/main.js:7415:34)
I’ve looked around on google and checked out the articles written by polyglot-developer but those examples seem outdated since they reference things that were part of the Beta and are no longer.
Here are my dependencies from package.json:
“dependencies”: {
"@angular/common": “2.2.1”,
"@angular/compiler": “2.2.1”,
"@angular/compiler-cli": “2.2.1”,
"@angular/core": “2.2.1”,
"@angular/forms": “2.2.1”,
"@angular/http": “2.2.1”,
"@angular/platform-browser": “2.2.1”,
"@angular/platform-browser-dynamic": “2.2.1”,
"@angular/platform-server": “2.2.1”,
"@ionic/storage": “^1.1.6”,
“ionic-angular”: “2.0.0”,
“ionic-native”: “2.2.11”,
“ionicons”: “3.0.0”,
“rxjs”: “5.0.0-beta.12”,
“zone.js”: “0.6.26”
},
“devDependencies”: {
"@ionic/app-scripts": “1.0.0”,
“typescript”: “2.0.9”
},
Here is where I first reference it in my app.module.ts
import { NgModule, ErrorHandler } from ‘@angular/core’;
import { IonicApp, IonicModule, IonicErrorHandler} from ‘ionic-angular’;
import { MyApp } from ‘./app.component’;
import { LandingPage } from ‘…/pages/landing/landing’;
import { Storage } from ‘@ionic/storage’;
import { UserSettingService } from ‘…/services/UserSettingService’;
@NgModule({
declarations: [
MyApp,
LandingPage
],
imports: [
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
LandingPage
],
providers: [
Storage,
//{ provide: Storage, useFactory: getStorage },
{ provide: ErrorHandler, useClass: IonicErrorHandler },
UserSettingService
]
})
export class AppModule {}
I have a service where I am trying to inject the storage:
export interface IUserSettingsService
{
retrieveUserSettings(
retrieveCallback: (settings: UserSettings) => void,
failCallback: (error: Error) => void
);
persistUserSettings(settings: UserSettings,
failCallback: (error: Error) => void
);
}
@Injectable()
export class UserSettingService implements IUserSettingsService
{
readonly USER_KEY: string = “user.settings”;
constructor(private storage: Storage) {
}
retrieveUserSettings(
retrieveCallback: (settings: UserSettings) => void,
failCallback: (error: Error) => void) {
this.storage.get(this.USER_KEY)
.then((val) => { retrieveCallback(val) })
.catch((error) => { failCallback(error) });
}
persistUserSettings(settings: UserSettings, failCallback: (error: Error) => void) {
this.storage.set(this.USER_KEY, settings)
.catch((error) => { failCallback(error) });
}
}
And then a page where I am trying to inject my service:
@Component({
selector: ‘landing’,
templateUrl: ‘landing.html’,
providers: [UserSettingService]
})
export class LandingPage {
userSettings: UserSettings;
constructor(public navCtrl: NavController, settingsService: UserSettingService) {
settingsService.retrieveUserSettings(this.onUserSettingsRetrieved, this.onUserSettingsFailed);
}
onUserSettingsRetrieved(userSettings: UserSettings) {
/// some business logi
}
onUserSettingsFailed(err:Error) {
/// report some error message
}
}
Please help.