其实我正在使用离子2和Secure Storage,我做了什么,首先
ionic plugin add cordova-plugin-secure-storage
安装插件后,
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Platform} from 'ionic-angular';
import { AlertController } from 'ionic-angular';
import { Facebook} from 'ionic-native';
import { SecureStorage } from 'ionic-native';
import { TourPage } from '../tour/tour';
import { HomePage } from '../home/home';
import { LoginService } from './login.service';
@Component({
selector: 'page-login',
templateUrl: 'login.html',
providers: [LoginService]
})
export class LoginPage {
tourPage = TourPage;
secureStorage:SecureStorage = new SecureStorage();
constructor(public nav: NavController,
public alert: AlertController,
public loginService:LoginService
) {
this.secureStorage.create('mibank').then(
() => console.log('Storage is ready!'),
error => console.log(error)
);
}
loginFb(){
...
var self = this;
self.loginService.register(token).subscribe(
(user)=>{ self.saveUser(user)},
error => console.error(`Error: ${error}`));
}
saveUser(user:any) {
console.log('save user...',user);
this.secureStorage.set('token',user.token).then(
data => {
return this.secureStorage.get('tourShown');
},
error => console.log(error)
).then( (tourShown)=> {
console.log('is tour shown?: ',tourShown);
if(tourShown){
this.nav.setRoot(HomePage);
}else{
this.nav.setRoot(TourPage);
}
});
}
}
我总是收到错误
Cannot read property 'set' of undefined(…)
上面的代码有什么问题?
答案 0 :(得分:0)
您需要确保在设备准备好后调用'secureStorage.create'。
例如,一旦platform.ready在app.component.ts中完成:
this.platform.ready().then(() => {
// Initialize your storage here
});