Best practice for changing route (VueRouter) after mutation (Vuex)

I searched a lot, but there is no clear answer. Basically, what should be the best practice for automatically changing a route after a mutation?

Ex: I press the button for logging in () → the login of the action that makes the HTTP call → mutation LOGIN_SUCCESSFUL → I want to redirect the user to the main page $router.go()

  • Do I have to transfer the action to Promise and then listen to the result to trigger a route change from the component?

  • Should I do it directly from $store?

  • Does it help vuex-router-sync?

Thank you so much!

+4
source share
2 answers

Vue.

( ) , , . , $store.

: , .

watch , , LOGGED_IN_USER ( ) . 100% , , , .

.

+2

app.vue, en . ,

App.vue:

import EventBus from './eventBus';

 methods: {   
    redirectURL(path) {
      this.$router.go(path)}
    },

 created() {
  EventBus.$on('redirect', this.redirectURL)
 }

:

import EventBus from './eventBus';

LOGIN_SUCCESSFUL() {
  state.blabla = "blabla";
  EventBus.$emit('redirect', '/dashboard')

}
0

Source: https://habr.com/ru/post/1667912/


All Articles