离子存储不是设置值

Ionic storage is not setting value

尝试使用离子存储设置值时,我总是得到未定义的值。 虽然我确实在 console.log

的第一个实例中看到了控制台中的值

查看代码中的输出值:

GeneratePBKDF2(pbkdf2Password) {

  console.log("New password created: " + pbkdf2Password);
  // Output: New password created:PBKDF2$sha2561G9D/PkC521fqOtgNgGt6rkhs5UeVlp2oJfI0l3CLpUgk6

  this.storage.set("TempPass", pbkdf2Password);
   // Error output: Uncaught TypeError: Cannot read property 'storage' of undefined

   console.log("Test Output" + pbkdf2Password);
   // Output: null
  }


Start() {
  var password = PasswordGenerator.generate({length: 20,numbers: true});
  MosquittoPBKDF.createPasswordAsync(password, this.GeneratePBKDF2);
  }

当您将方法添加为参数时,您的 this 没有指向组件范围。通过粗箭头尝试这种方法:

GeneratePBKDF2 = (pbkdf2Password) => {

  console.log("New password created: " + pbkdf2Password);
  // Output: New password created:PBKDF2$sha2561G9D/PkC521fqOtgNgGt6rkhs5UeVlp2oJfI0l3CLpUgk6

  this.storage.set("TempPass", pbkdf2Password);
   // Error output: Uncaught TypeError: Cannot read property 'storage' of undefined

   console.log("Test Output" + pbkdf2Password);
   // Output: null
  }