我的存储实现离子2应用程序有什么问题?

时间:2016-10-24 22:09:43

标签: ionic-framework local-storage storage ionic2

我试图在离子2 app的本地存储中保存数据,所以我 导入存储并完全像我在网站上看到的那样,并没有将数据保存在存储

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 个答案:

答案 0 :(得分:1)

安装

要在您的Ionic 2 / Angular 2应用程序中使用它,要么启动一个新的Ionic项目,默认安装它,或运行:

npm install @ionic/storage

如果您想使用SQLite作为存储引擎,请安装SQLite插件(仅在模拟器或设备上运行时有效):

cordova plugin add cordova-sqlite-storage --save

要使用存储,您可能需要在src / app / app.module.ts中编辑NgModule声明,以便将存储添加为提供者,如下所示:

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(key,value):

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

要获取该项目,请使用Storage.get(name).then((value)=&gt; {})因为get()返回一个Promise:

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