【问题标题】:How to use $navigateTo in natviescript-vue vuex store actions?如何在 natviescript-vue vuex 商店操作中使用 $navigateTo?
【发布时间】:2020-01-10 14:17:09
【问题描述】:

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

【问题讨论】:

    标签: vue.js nativescript vuex nativescript-vue


    【解决方案1】:

    您可以从存储操作中返回一个值。由于操作是异步的,因此您需要处理生成的承诺,例如

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

    这里解释了这个概念: https://vuex.vuejs.org/guide/actions.html#composing-actions

    【讨论】:

      【解决方案2】:

      我是这样解决的:

      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);
      },
      

      【讨论】:

        猜你喜欢
        • 2019-08-18
        • 2017-12-18
        • 2021-12-05
        • 2017-07-22
        • 1970-01-01
        • 2020-01-01
        • 2021-10-20
        • 1970-01-01
        • 2018-12-19
        相关资源
        最近更新 更多