无法在IONIC中的另一个类app.component.ts上获得存储变量

时间:2018-07-24 14:36:43

标签: ionic-framework local-storage

我正在开发离子版本3.20.0的应用程序。

那样,我具有使用Facebook登录的功能。

用户成功登录Facebook后,我正在login.ts中用this.storage.set('picture', data['picture']['data']['url']);设置存储变量

我可以使用下面的代码成功在另一个名为dashboard.ts的文件中获取此图片变量

 storage.get('picture').then((imagestore) => {
      console.log(imagestore);
      if(imagestore){ console.log("IF call");
          this.profilePhoto = imagestore;
     }else{
         this.profilePhoto = "./assets/images/profile.png";
          console.log("Else call");
      }     
    });

但是我无法在app.components.ts中获取图片存储变量。

任何帮助将不胜感激。

下面是我的app.components.ts文件

  

import {Component,ViewChild}   来自'@ angular / core';导入{Platform,MenuController,Nav,App}   来自“离子角”;从导入{SplashScreen}   '@ ionic-native / splash-screen';从导入{StatusBar}   '@ ionic-native / status-bar';从“ @ ionic / storage”导入{Storage};   从导入{Facebook,FacebookLoginResponse}   '@ ionic-native / facebook';

     

从'@ ionic-native / secure-storage'导入{SecureStorage};导入{   来自“ ../pages/landing/landing”的LandingPage};导入{DashboardPage   }来自“ ../pages/dashboard/dashboard”;从导入{MarketPage}   '../pages/market/market';从导入{ImagePicker}   '@ ionic-native / image-picker';

     

@Component({选择器:'app-root',templateUrl:'app.html'})   导出类MyApp {       @ViewChild(Nav)导航:Nav;公共名称:任何;公共fbphotoshidden:任何; public firstName:任何;公开个人资料照片:   任何;

     

页面:数组<{title:字符串,图标:字符串,组件:任何}>;
  pushPages:Array <{title:字符串,icon:字符串,组件:任何}>;

     

构造函数(

platform: Platform,
private secureStorage: SecureStorage,
private storage: Storage,
public menu: MenuController,
private fb: Facebook,
public app: App,
public splashScreen: SplashScreen,
public statusBar: StatusBar,
public imagePicker: ImagePicker
     

){

platform.ready().then(() => {
  // Okay, so the platform is ready and our plugins are available.
  // Here you can do any higher level native things you might need.
  this.splashScreen.hide();
  this.statusBar.styleDefault();
  this.statusBar.overlaysWebView(true);

  this.secureStorage.create('jeffrey').then(
    () => {
      console.log("Secure Storage is ready");
    },
    (error) => {
      //there should be screen lock available in your application with pin or pattern
      console.log(error);
    }
  );

});


storage.get('firstName').then((store) => {

        if(store){
            this.firstName = store;
        }

    });
console.log("Storage Picture");   console.log( storage.get('picture'));
storage.get('picture').then((imagestore) => {
  console.log(imagestore);
  if(imagestore){         console.log("IF call");
      this.profilePhoto = imagestore;
  }else{
      this.profilePhoto = "./assets/images/profile.png";
      console.log("Else call");
  }

});
storage.get('login').then((store) => {

  if(store){ 
    this.nav.setRoot(DashboardPage); 
  }else{
    this.nav.setRoot(LandingPage); 
  }

});
     

}

     

异步selectImage(){       让options = {         //要选择的最大图像数,默认为15。如果设置为1,         //选择单个图像,插件将返回它。         maximumImagesCount:1,

  // max width and height to allow the images to be.  Will keep aspect
  // ratio no matter what.  So if both are 800, the returned image
  // will be at most 800 pixels wide and 800 pixels tall.  If the width is
  // 800 and height 0 the image will be 800 pixels wide if the source
  // is at least that wide.
  //width: 100,
  //height: 100,

  // quality of resized image, defaults to 100
  //quality: 100
};

const [imageSource] = await this.imagePicker.getPictures(options);
this.storage.set('picture', imageSource.replace('file://', ''));
console.log(this.profilePhoto);
     

} goHome(){       this.menu.close();       this.nav.setRoot(DashboardPage); }        去市场(){       this.menu.close();       this.nav.setRoot(MarketPage); }

     

checkFbLogin(){       this.fb.getLoginStatus()。then((status:FacebookLoginResponse)=> {

  let login = status.status;
  if(login == 'connected'){

      this.storage.get('picture').then((pic) => {

          if(pic){
              this.profilePhoto = pic;
          }else{
              this.profilePhoto = "./assets/images/profile.png";
          }

      });

  }else{
         this.profilePhoto = "./assets/images/profile.png";
  }

})

}
     

openPage(page){       //单击菜单中的链接时关闭菜单       this.menu.close();       //如果不是当前页面,请导航到新页面       this.nav.setRoot(page.component); }

     

注销(事件){          this.menu.close();         this.storage.clear();         this.nav.setRoot(LandingPage); }

     

pushPage(page){       //单击菜单中的链接时关闭菜单       this.menu.close();

this.app.getRootNav().push(page.component);
     

}}

1 个答案:

答案 0 :(得分:0)

我已经通过离子事件解决了这个问题。

感谢您检查此问题。