Home

Issue: 1384

What is the best way to access this.$router.push() / router.push() inside the Vuex?

[issue link]

This seems a silly question but I’m really struggling right now to accomplish a simple router.go('/') inside store/index.js. Basically I want to change the page after I complete the authentication. I’m using this example as base.

I know const router = require('~router'); isn’t working anymore since rc-3 removed the alias to ~router.

I tried access this.$router but isn’t available either. I also tried import router from 'vue-router'; with no luck 😦


Here a partial of my store/index.js:

export const actions = {
  // Login
  async login({ commit }, { email, password }) {
    await axios.post('/api/account/login', {
      email,
      password,
    })
      .then((response) => {
        console.log('response:', response);

        if (response.status === 200) {
          console.log('>> Login successfull');
          commit('SET_USER', response.data);
          sessionStorage.setItem('user', JSON.stringify(response.data));

          //I can't make work!
          Router.push('/about');
        } else if (response.status === 204) {
          console.log('>> Username password do not match');
        } else {
          console.log('>> Username does not exists');
        }
      })
      .catch((error) => {
        commit('SET_USER', null);

        console.log('OPS!');
        console.log(error);
      });
  },
};

I appreciate any help… Thanks in advance!

This question is available on Nuxt.js community (#c1227)