【问题标题】:securestorage ionic not access after record记录后无法访问securestorage ionic
【发布时间】:2018-04-17 03:09:05
【问题描述】:

我读过Ionic,检查一下code,这里也是Sample,我总是失败。 :(。 这是我的代码:

export class HomePage {
 private secureStorage: SecureStorage;
 private data: SecureStorageObject;
 private msg: string;
 constructor(public navCtrl: NavController, public platform: Platform, 
 private toast: ToastController) {
if (platform.is('cordova')) {
  platform.ready().then(() => {

    this.secureStorage = new SecureStorage();
    this.secureStorage.create('demoapp').then(
      (storage: SecureStorageObject) => {
        this.toast.create({message: 'Storage is ready!',position: 'bottom', duration: 3000}).present();
        this.data = storage;
        this.data.set('teste','chave')
          .then(
            data => {
              this.setMsg("gravou");
              this.toast.create({message: "enable ="+this.msg,position: 'bottom', duration: 3000}).present();
              this.data.get('teste')
                .then(
                  data => {
                    this.setMsg(data);
                    this.toast.create({message: "content="+this.msg,position: 'bottom', duration: 3000}).present();
                  },
                  error => {
                    // do nothing - it just means it doesn't exist
                  }
                );
            },
            error => {
              // do nothing - it just means it doesn't exist
            }
          );
      },
      error => console.log(error)
    );
  });
} else {
  this.toast.create({message: "not enable",position: 'bottom', duration: 3000}).present();
}
}
setMsg(msg: string) {
  this.msg = msg;
}

getMsg(){
  return this.data.get('teste');
}

showData() {
  this.toast.create({message: "show ="+this.getMsg(),position: 'bottom', duration: 3000}).present();
}

上面的代码在“.then(...)”中运行 toast 时正确返回 Key。但是,当尝试在其他地方访问时,例如 showData(),不起作用,返回“未定义”。

为什么“this.data = storage”不能在“create('demoapp').then(...)”之外工作?

如何在其他页面访问存储在“demmoapp”中的密钥?

如果有我可以遵循的教程修复,请与我分享。 我现在从 ionic 开始,开始研究 angular 和 ionic,但我还没有为这个问题做好准备。

运行时间:

#ng -v
Unable to find "@angular/cli" in devDependencies.

Please take the following steps to avoid issues:
"npm install --save-dev @angular/cli@latest" 


    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/

Angular CLI: 1.7.4
Node: 8.11.1
OS: darwin x64
Angular: 5.2.9
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic


@angular-devkit/build-optimizer: 0.0.35
typescript: 2.6.2
webpack: 3.8.1

#ionic -v
3.20.0

多合一 mac os x sierra。

感谢

【问题讨论】:

  • 是有错误,还是没有数据?
  • 返回“未定义”。

标签: angular ionic-framework mobile


【解决方案1】:

我没有时间重复这个问题,所以如果我有时间我会添加更多。

我注意到您在同一范围内使用了两次变量数据。

嵌套promise时你应该改变变量名。

    this.data.set('teste','chave')
      .then(
        data => {

          // !! FIRST DEFINITION OF DATA  ^^   

          this.setMsg("gravou");
          this.toast.create({message: "enable ="+this.msg,position: 'bottom', duration: 3000}).present();
          this.data.get('teste')
            .then(
              data => {

                // !! SECOND DEFINITION OF DATA ^^

                this.setMsg(data);
                this.toast.create({message: "content="+this.msg,position: 'bottom', duration: 3000}).present();
              },
              error => {
                // do nothing - it just means it doesn't exist
              }
            );
        },
        error => {
          // do nothing - it just means it doesn't exist
        }
      );

另外,你在调试这个吗?您正在单步执行您的代码吗?

【讨论】:

  • 我按照您的建议更改了变量名称,同样的错误。我如何使用存储,我的测​​试直接在我的手机 android 上。我正在使用 webstorm,我该如何与他一起调试?
  • 我确信有一种方法可以在 android 上进行调试,但只需在浏览器中进行即可。运行 'ionic serve' 并在第一个 'data' 承诺值之后添加调试器语句。然后在您使用的任何浏览器中进行检查。
  • 我放弃了,在网上找不到任何帮助我的东西。我找到了这个(cloudypoint.com/Tutorials/discussion/…)但没有工作。我将更改为本地存储。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-03
  • 2011-08-19
  • 2016-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多