如何从 Vuex 操作使用 vue 路由器进行导航 [英] How to navigate using vue router from Vuex actions

查看:25
本文介绍了如何从 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:"/"}) 给了我以下错误.

But this.$router.push({path:"/"}) gives me following error.

未捕获(承诺)类型错误:无法读取未定义的属性推送"

Uncaught (in promise) TypeError: Cannot read property 'push' of undefined

这是如何实现的.

模拟 JsFiddle:这里

Simulated JsFiddle: here

推荐答案

import router from './router'

并使用router.push

就这么简单.

这篇关于如何从 Vuex 操作使用 vue 路由器进行导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆