How to navigate using vue router from Vuex actions

前端 未结 5 457
既然无缘
既然无缘 2020-12-03 06:12

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 t

5条回答
  •  失恋的感觉
    2020-12-03 06:51

    I didn't like keeping my app's location state separate from the rest of my app state in the Store, and having to manage both a Router and a Store, so I created a Vuex module that manages the location state inside the Store.

    Now I can navigate by dispatching actions, just like any other state change:

    dispatch("router/push", {path: "/error"})
    

    This has the added benefit of making things like animated page transitions easier to handle.

    It's not hard to roll your own router module, but you can also try mine if you want to:

    https://github.com/geekytime/vuex-router

提交回复
热议问题