nativescript 等待请求直到 vuex 完成请求
nativescript wait for request until vuex complete request
我有页面 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
早
谢谢
为了在检查 getter 之前正确等待并触发忙碌状态,return 来自 attempt
操作的承诺:
attempt({ state, commit }) {
return axios.post(...) // <-- Returning the promise manually
.then(response => {
// Commit change
})
},
或使用异步/等待:
async attempt({ state, commit }) { // <-- async keyword returns promise automatically
const response = await axios.post(...);
// Commit change
}
这是一个demo
我有页面 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
早
谢谢
为了在检查 getter 之前正确等待并触发忙碌状态,return 来自 attempt
操作的承诺:
attempt({ state, commit }) {
return axios.post(...) // <-- Returning the promise manually
.then(response => {
// Commit change
})
},
或使用异步/等待:
async attempt({ state, commit }) { // <-- async keyword returns promise automatically
const response = await axios.post(...);
// Commit change
}
这是一个demo