2016-10-21 71 views
2

其實我與離子2和Secure Storage工作,我做什麼,首先是無法讀取屬性未定義(...)安全存儲離子的「設置」 2

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

您需要確保在設備準備就緒後調用「secureStorage.create」。

例如,一旦platform.ready已app.component.ts完成:

this.platform.ready().then(() => { 
    // Initialize your storage here 
}); 
相關問題