angularjs - 离子存储 GET
问题描述
我尝试从离子存储中获取价值,但在这里不起作用。为什么 GET2 在 storage.get 之前执行?我脑子坏了,求救。
public storageGet(key: string){
var uid = 0;
this.storage.get(key).then((val) => {
console.log('GET1: ' + key + ': ' + val);
if (val != null) { uid = val;}
});
console.log('GET2: ' + key + ': ' + uid);
return uid;
}
返回:
GET2: uid: 0
GET1: uid: 1
解决方案
你需要了解 Promise 是如何工作的。
这段代码是异步的,回调中的所有行then
都会被执行,但你不能决定什么时候。
console.log("GET2")
严格在 之后执行,这storage.get
部分是同步的。
推荐阅读
- django - 没有在 django api 视图中获取外键数据
- javascript - 验证输入日期超过 18 年的问题
- react-native - 带有预填充文本的应用内信使 API
- java - Java - 淡化和显示图像的不同方法
- mysql - mysql:如何创建一个从另一个表中查找值的计算字段
- node.js - Heroku在错误的文件夹中搜索index.html,如何解决?
- c# - 上传时自定义 HttpContent 失败
- reactjs - 使用 React Navigation 导航堆栈时重新渲染组件
- javascript - 使用 iframe 嵌入同一页面而不创建 iframe 的无限循环
- libvirt - 如何恢复 libvirt 网络默认配置文件?