如何在actions中调用router?

actions: {

    handleBack () {
      this.route.go(-1)
    }

  }
methods: {

      handleBack: function () {
        this.$store.dispatch('handleBack')
      }
}

请问,怎样才能在actions中调用router?

very much Thanks in advance~~~

阅读 4.6k
1 个回答

routerstore 一樣都可以手動暴露出來直接調用,例如

app.js

// 路由設定
var router = new VueRouter({
    mode: 'history',
    root: '/dashboard',
    linkActiveClass: 'active',
    routes
})

router.beforeEach((to, from, next) => {
    console.log(to.path)
    next()
})

new Vue({
    el: '#app',
    router,
    store,
    render: h => h(require('./components/App.vue'))
})

// 暴露出來
export { router, store }

那在任何地方都可以進行調用,其實我們把 store, router 放進 new Vue() 裡面也只是單純注入盡各組件而已,本質上沒啥差別。

import { router, store } from '../app.js'

router.push('/some/where')
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题