我读了Ionic,检查这个代码,这里也是样本,我总是失败。 :(。这是我的代码:
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 时正确返回密钥。但是,当尝试在其他地方访问时,例如 showData((,而不是工作,返回"undefined"。
为什么"this.data = storage"在"create('demoapp'(.then(...("之外不起作用?
如何在其他页面中访问存储在"demmoapp"中的此密钥?
如果有一个教程可以修复,请与我分享。我现在从离子开始,开始研究角度和离子,但我还没有为这个问题做好准备。
运行于:
#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.
谢谢
我没有时间重复这个问题,所以如果我有时间,我会添加更多内容。
我确实注意到您在同一范围内两次使用变量数据。
嵌套 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
}
);
另外,您是否正在调试此内容?您是否正在逐步执行代码?