如何在 natviescript-vue vuex 存储操作中使用 $navigateTo?

How to use $navigateTo in natviescript-vue vuex store actions?

this.$navigateTo 在我的组件的方法中工作得很好,但在突变中 Vue.$navigateTothis.$navigateTo 都不起作用。我的导航取决于我从 api 调用中获得的结果,如果无法从商店操作中执行导航,我如何从商店操作中获取一些 return 值以便我可以执行我的组件内的导航?

我是这样解决的:

new Vue({
  store,
  render: h => h('frame', [h(store.state.is_logged_in ? App : Login)]),
  created() {
    this.$store.commit('setNav', t => this.$navigateTo(t));
    if (this.$store.state.is_logged_in) {
      this.$store.dispatch('init');
    }
  },
}).$start();

现在我的行​​动是:

logout({commit, state}) {
  console.log('logged out');
  commit('log_out');
  state.nav(Login);
},

您可以 return 来自存储操作的值。由于操作是异步的,因此您需要处理生成的承诺,执行类似

的操作
store.dispatch('actionA').then((target) => {
  // navigate to target
})

概念解释如下: https://vuex.vuejs.org/guide/actions.html#composing-actions