我正在为学校项目制作Ionic 3应用程序,我在加载用于将内容存储在本地存储中的提供程序时遇到问题,例如用户令牌,首选项和其他与客户端相关的内容>
这是提供者的基本代码
import {Storage} from '@ionic/storage';
import {IToken, IUser} from "../../interfaces";
import {Injectable} from '@angular/core';
import {Events} from 'ionic-angular';
import {ApiProvider} from "../api/api";
@Injectable()
export class GeneralProvider {
private static USER = 'user';
private static TOKEN = 'token';
constructor(private storage: Storage,
private events: Events,
private api: ApiProvider) {
}
private set(settingName: string, value: any) {
return this.storage.set(`setting:${ settingName }`, value);
}
private async get(settingName: string): Promise<any> {
return await this.storage.get(`setting:${ settingName }`)
}
private remove(settingName: string) {
return this.storage.remove(`setting:${ settingName }`);
}
/******************************* TOKEN ******************************/
public setToken(token: IToken):void {
this.set(GeneralProvider.TOKEN, token);
}
public getToken(): Promise<IToken> {
return this.get(GeneralProvider.TOKEN);
}
public clearToken():void {
this.remove(GeneralProvider.TOKEN);
}
/******************************* USER ******************************/
public setUser(user: IUser) {
return this.set(GeneralProvider.USER, user);
}
public getUser(): Promise<IUser> {
return this.get(GeneralProvider.USER);
}
public async isLoggedIn() {
let user = await this.getUser();
return user !== null;
}
public logout() {
return new Promise(async (resolve) => {
this.api.logout()
.then(async response => {
await this.remove(GeneralProvider.TOKEN);
await this.remove(GeneralProvider.USER);
this.events.publish('user:loggedout');
})
})
}
}
这是我的app.module.ts
/* imports */
@NgModule({
declarations: [
MyApp,
HomePage,
AddPlacePage,
PlacePage,
SetLocationPage,
TabsPage,
RegisterPage,
SettingsPage,
RegisterComponent,
LoginComponent
],
imports: [
FormsModule,
BrowserModule,
MbscModule,
IonicModule.forRoot(MyApp),
IonicStorageModule.forRoot(),
],
bootstrap: [IonicApp],
entryComponents: [
...
],
providers: [
PlacesService,
ApiProvider,
GeneralProvider,
ImagesProvider,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {
}
这是我得到的错误
Uncaught ReferenceError: GeneralProvider is not defined
at Object.a (location.ts:3)
at Object.48 (api.ts:12)
at __webpack_require__ (bootstrap c115de5b48ba66ac336f:54)
at Object.61 (location.ts:3)
at __webpack_require__ (bootstrap c115de5b48ba66ac336f:54)
at Object.377 (main.js:138)
at __webpack_require__ (bootstrap c115de5b48ba66ac336f:54)
at Object.376 (main.js:52)
at __webpack_require__ (bootstrap c115de5b48ba66ac336f:54)
at Object.549 (app.module.ts:72)
这是因为location.ts
只是一个通用接口,它定义了某些对象的结构,因此我高度怀疑问题是否在那里。在过去的一天里,我一直在为此绞尽脑汁,没有任何变化。