vue.js - nativescript 等待请求,直到 vuex 完成请求
问题描述
我有页面 Login.vue,如果用户已经登录,我正在使用一种策略,然后转到主页组件,否则保持不变
我的代码
mounted() {
this.checkAlreadyLoggedIn();
},
methods: {
async checkAlreadyLoggedIn() {
this.busy = true;
await this.$store.dispatch("attempt");
this.busy = false;
if (this.$store.getters.loggedIn) {
this.$navigateTo(Home, {
clearHistory: true
});
}
},
}
尝试对服务器的操作请求并获取用户详细信息,但它似乎很this.$store.getters.loggedIn
早就触发了
谢谢
解决方案
In order to wait properly before checking the getter, and trigger the busy state, return the promise from the attempt
action:
attempt({ state, commit }) {
return axios.post(...) // <-- Returning the promise manually
.then(response => {
// Commit change
})
},
Or with async / await:
async attempt({ state, commit }) { // <-- async keyword returns promise automatically
const response = await axios.post(...);
// Commit change
}
Here is a demo
推荐阅读
- accessibility - 可访问性和链接是按钮,反之亦然
- sql - 不带 @ 符号的 SQL 存储过程的参数输入
- javascript - 反应路由器访问对象详细信息
- r - R data.table :使用 .N 创建多列值的计数表
- ios - Swift - 再次呈现 ViewController 后推动 ViewController 不起作用
- amazon-dynamodb - DynamoDB Schema - 对建筑物内的位置进行建模
- flutter - 在 Flutter 中强制纵向方向时出错
- r - 如何将此 csv 导入到 R
- python - 如何在 django 中加密\解密文件\图像字段?
- asp.net-core - asp.net webapi core 3.1.self-hosted Kestrel 应用程序需要转换为 WIndows 服务