我正在尝试为Ionic 2配置Angular2-JWT。在我更新 Ionic 之前,以下代码有效。现在没有了。我知道对 Ionic Storage 进行了更改,这导致了问题,但我不明白如何转换以下代码:
import { BrowserModule } from '@angular/platform-browser';
import { Http } from '@angular/http';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { AuthHttp, AuthConfig } from 'angular2-jwt';
import { ElasticModule } from 'angular2-elastic';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { NumberBoard } from '../components/number-board/number-board';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { CloudSettings, CloudModule } from '@ionic/cloud-angular';
import { IonicStorageModule, Storage } from '@ionic/storage';
let storage: Storage = new Storage();
export function getAuthHttp(http) {
return new AuthHttp(new AuthConfig({
headerName: 'token',
noTokenScheme: true,
globalHeaders: [{'Accept': 'application/json'}],
tokenGetter: (() => storage.get('token'))
}), http);
};
const cloudSettings: CloudSettings = {
'core': {
'app_id': 'c3890396'
}
};
@NgModule({
declarations: [
MyApp,
HomePage,
NumberBoard
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
CloudModule.forRoot(cloudSettings),
IonicStorageModule.forRoot()
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
{provide: AuthHttp, useFactory: getAuthHttp, deps: [Http]},
]
})
export class AppModule {}
Package.json:
{
"name": "ionic-hello-world",
"author": "Ionic Framework",
"homepage": "http://ionicframework.com/",
"private": true,
"scripts": {
"clean": "ionic-app-scripts clean",
"build": "ionic-app-scripts build",
"ionic:build": "ionic-app-scripts build",
"ionic:serve": "ionic-app-scripts serve"
},
"dependencies": {
"@angular/common": "4.0.0",
"@angular/compiler": "4.0.0",
"@angular/compiler-cli": "4.0.0",
"@angular/core": "4.0.0",
"@angular/forms": "4.0.0",
"@angular/http": "4.0.0",
"@angular/platform-browser": "4.0.0",
"@angular/platform-browser-dynamic": "4.0.0",
"@ionic-native/core": "3.4.2",
"@ionic-native/crop": "^3.4.4",
"@ionic-native/image-picker": "^3.4.4",
"@ionic-native/splash-screen": "3.4.2",
"@ionic-native/status-bar": "3.4.2",
"@ionic/cloud-angular": "^0.11.0",
"@ionic/storage": "2.0.1",
"angular2-elastic": "^0.13.0",
"angular2-jwt": "^0.2.0",
"ionic-angular": "3.0.0",
"ionicons": "3.0.0",
"rxjs": "5.1.1",
"sw-toolbox": "3.4.0",
"zone.js": "^0.8.4"
},
"devDependencies": {
"@ionic/app-scripts": "1.3.0",
"typescript": "~2.2.1"
},
"cordovaPlugins": [
"cordova-plugin-whitelist",
"cordova-plugin-console",
"cordova-plugin-statusbar",
"cordova-plugin-device",
"cordova-plugin-splashscreen",
"ionic-plugin-keyboard"
],
"cordovaPlatforms": [],
"description": "Rediyal-Business: An Ionic project"
}
看起来像:
let storage: Storage = new Storage();
在 2.0.0 及更高版本中不起作用。检查离子存储代码。它的构造函数接受一个参数。
constructor(config: StorageConfig)
不过,您应该能够通过注入像提供程序这样的构造函数来使用它。
constructor(private storage: Storage)
此处设置了依赖关系注入。
{ provide: Storage, useFactory: provideStorage, deps: [StorageConfigToken]},
也试试
let storage: Storage = new Storage(null);