2016-10-24 44 views
2

我試圖在本地存儲數據保存在離子2應用程序,所以我 導入存儲和準確不喜歡我在網站上看到,它不將數據保存在存儲的我的存儲實施離子2應用程序有什麼問題?

import { Component} from '@angular/core'; 

import { NavController,NavParams,LoadingController,AlertController,ViewController } from 'ionic-angular'; 
import { Facebook } from 'ionic-native'; 

//import pages 
import {LoginPage} from "../../pages/login/login"; 
import {User} from '../../models/user' 
import { Storage} from '@ionic/storage'; 
//import provider 
import { ProfileData } from '../../providers/profile-data'; 
import { NotesData } from '../../providers/notes-data'; 

import firebase from 'firebase' 
import {AddNote} from "../add-note/add-note"; 

@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 

export class HomePage { 

    pages: Array<{title: string, component: any}>; 
    photo:any; 




    constructor(public navCtrl: NavController,public storage:Storage) { 


    } 
    ionViewDidLoad() { 

    this.getDetailsFacebook(); 
    } 

    getDetailsFacebook() { 
    var that=this; 
    Facebook.getLoginStatus().then((response)=> { 
     if (response.status == 'connected') { 
     Facebook.api('/' + response.authResponse.userID + '?fields=id,name,gender', []).then((response)=> { 
     that.uid = response.id; 
     that.photo = "http://graph.facebook.com/"+that.uid+"/picture?type=large"; 
      that.storage.set('photo',that.photo'); 

      //console.log("id:"+this.uid+this.name+this.photo); 
     }, (error)=> { 
      alert(error); 
     }) 
     } 
     else { 
     alert('Not Logged in'); 
     } 
    }) 

照片與鉻開發商檢查 enter image description here

我沒有看到任何照片的關鍵,因爲我設置它..爲什麼呢?

回答

1

安裝

要在離子2 /角2個應用程序使用它,或者啓動已默認情況下它安裝了一個新鮮離子項目,或運行:

npm install @ionic/storage 

如果你想使用SQLite作爲存儲引擎,安裝一個SQLite插件(在模擬器或設備上運行時只適用):

cordova plugin add cordova-sqlite-storage --save 

爲了使用存儲您可能需要編輯NgModule德claration在SRC /應用/ app.module.ts增加存儲的供應商如下:

import { Storage } from '@ionic/storage'; 

@NgModule({ 
    declarations: [ 
    ... 
    ], 
    imports: [ 
    IonicModule.forRoot(MyApp) 
    ], 
    bootstrap: [IonicApp], 
    entryComponents: [ 
    ... 
    ], 
    providers: [ Storage ] // Add Storage as a provider 
}) 
export class AppModule {} 

現在,您可以輕鬆地將存儲到一個組件:

import { Component } from '@angular/core'; 

import { NavController } from 'ionic-angular'; 

import { Storage } from '@ionic/storage'; 

@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 
export class HomePage { 

    constructor(public navCtrl: NavController, public storage: Storage) { 

    } 

} 

要設置的項目,使用Storage.set(鍵,值):

this.storage.set('name', 'Mr. Ionitron'); 

要獲取項背,使用Storage.get(名稱)。然後((值)=> {}),因爲get()返回一個承諾:

this.storage.get('name').then((name) => { 
    console.log('Me: Hey, ' + name + '! You have a very nice name.'); 
    console.log('You: Thanks! I got it for my birthday.'); 
}); 

有關存儲模塊的更多信息,請參閱鏈接:https://github.com/driftyco/ionic-storage

相關問題