【发布时间】:2020-01-10 14:17:09
【问题描述】:
this.$navigateTo 在我的组件的方法中工作得非常好,但在一个突变中,Vue.$navigateTo 和 this.$navigateTo 都不起作用。我的导航取决于我从 api 调用获得的结果,如果无法从商店操作中执行导航,我如何从商店操作中获取一些返回值,以便我可以在我的组件中执行我的导航?
【问题讨论】:
标签: vue.js nativescript vuex nativescript-vue
this.$navigateTo 在我的组件的方法中工作得非常好,但在一个突变中,Vue.$navigateTo 和 this.$navigateTo 都不起作用。我的导航取决于我从 api 调用获得的结果,如果无法从商店操作中执行导航,我如何从商店操作中获取一些返回值,以便我可以在我的组件中执行我的导航?
【问题讨论】:
标签: vue.js nativescript vuex nativescript-vue
您可以从存储操作中返回一个值。由于操作是异步的,因此您需要处理生成的承诺,例如
store.dispatch('actionA').then((target) => {
// navigate to target
})
这里解释了这个概念: https://vuex.vuejs.org/guide/actions.html#composing-actions
【讨论】:
我是这样解决的:
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);
},
【讨论】: