我正在将Storage迁移到SecureStorage。普通存储可以正常工作,但是我在所有平台上的安全存储都遇到问题。
我安装了here所述的插件。但是,如果我在浏览器或Android上运行该应用程序,则会出现以下错误:
plugin_not_installed
(...)
ERROR Error: "Uncaught (in promise): TypeError: this.storage is undefined
[42]/DataProvider.prototype.getLoginData
我将SecureStorage作为提供程序导入了app.module.ts
。
app.component.ts
看起来像这样:
constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, private dataProvider: DataProvider, private restProvider: RestProvider) {
this.initializeApp();
// ...
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.dataProvider.getLoginData().then((loginData) => {
console.log(loginData);
}, (error) => {
console.error(error);
});
}
DataProvider看起来像这样:
import {Injectable} from '@angular/core';
import {SecureStorage, SecureStorageObject} from '@ionic-native/secure-storage';
/**
* @see https://ionicframework.com/docs/native/secure-storage/
* @see https://github.com/Crypho/cordova-plugin-secure-storage
*/
@Injectable()
export class DataProvider {
storage: SecureStorageObject;
constructor(private secureStorage: SecureStorage) {
this.secureStorage.create('com.mydomain.myapp').then((storage: SecureStorageObject) => {
DevProvider.logDebugMsg('secureStorage created');
this.storage = storage;
}, (error) => {
console.error('creating secureStorage failed: ', error); // output is: plugin_not_installed
});
}
getLoginData() {
return this.storage.get('loginData');
}
}
这是怎么了?
谢谢。