如何使用Vuex动作中的Vue路由器进行导航 [英] How to navigate using vue router from Vuex actions
本文介绍了如何使用Vuex动作中的Vue路由器进行导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Vue 2.x和Vuex 2.x创建一个Web应用程序.我正在通过http调用从远程位置获取一些信息,我希望如果该调用失败,我应该重定向到其他页面.
I am creating a web app with Vue 2.x and Vuex 2.x. I am fetching some information from a remote location via an http call, I want that if that call fails I should redirect to some other page.
GET_PETS: (state) => {
return $http.get('pets/').then((response)=>{
state.commit('SET_PETS', response.data)
})
},
error => {this.$router.push({path:"/"}) }
)
}
但是this.$router.push({path:"/"})
给我以下错误.
未捕获(承诺)TypeError:无法读取未定义的属性"push"
Uncaught (in promise) TypeError: Cannot read property 'push' of undefined
这如何实现.
模拟的JsFiddle:此处
Simulated JsFiddle: here
推荐答案
import router from './router'
并使用router.push
那样简单.
这篇关于如何使用Vuex动作中的Vue路由器进行导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文