【问题标题】:nativescript wait for request until vuex complete requestnativescript 等待请求,直到 vuex 完成请求
【发布时间】:2020-07-22 19:25:31
【问题描述】:

我有页面 Login.vue,如果用户已经登录,我正在使用一种策略,然后转到 Home 组件,否则保持不变

我的代码

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

谢谢

【问题讨论】:

    标签: vue.js vuejs2 nativescript vuex nativescript-vue


    【解决方案1】:

    为了在检查 getter 之前正确等待,并触发忙碌状态,请从 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多