0

So, i want to vue router.push on my store.js, but i keep getting error Cannot read property 'push' of undefined

i've tried to import vue-router in my store.js, but still in vain

here's my app.js :

import Vue from 'vue'
import VueRouter from 'vue-router'

//Import and install the VueRouter plugin with Vue.use()
Vue.use(VueRouter)

import App from './views/App'
import Home from './views/Home'
import Login from './views/Login.vue'
import Register from './views/Register.vue'

const router = new VueRouter({
    mode: 'history',
    routes: [{
            path: '/',
            name: 'home',
            component: Home,
            meta: { requiresAuth: true }
        },
        {
            path: '/login',
            name: 'login',
            component: Login
        },
        {
            path: '/register',
            name: 'register',
            component: Register
        },
    ],
});



const app = new Vue({
    el: '#app',
    components: { App },
    router,
});

and here's my store.js :

import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        accessToken: null,
        loggingIn: false,
        loginError: null
    },

    mutations: {
        loginStart: state => state.loggingIn = true,
        loginStop: (state, errorMessage) => {
            state.loggingIn = false;
            state.loginError = errorMessage;
        },
        updateAccessToken: (state, accessToken) => {
            state.accessToken = accessToken;
        },
        logout: (state) => {
            state.accessToken = null;
        }
    },

    actions: {
        doLogin({ commit }, loginData) {
            commit('loginStart');
            console.log(loginData)

            axios.post('http://localhost:8000/api/login', loginData)
                .then(response => {
                    console.log(response)
                    let accessToken = response.data.jwt;
                    document.cookie = 'jwt_access_token=' + accessToken;
                    commit('updateAccessToken', accessToken);

                    ///this caused the error
                    this.$router.push({ path: '/' })
                })
                .catch(error => {
                    // commit('loginStop', error.response.data.error);
                    console.log(error)
                    commit('updateAccessToken', null);
                    console.log(error.response);
                })
        }
    }
})

as you can see, after i call doLogin() function, and using axios, it stop at the this.$router.push({ path: '/' }) line, causing error.

2

1 Answer 1

1

You need to make a router.js file

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  ...
})

export default router

In app.js replace the import of the vue-router to your new router.js and remove Vue.use(Router).

In the store, this is not the Vue instance. Import the router.js in your store.js;

import router from './router'

Then you can access it like this;

router.push({ path: '/' })

I also noticed that you haven't add the store to the Vue instance. Import and add in app.js.

import store from './store'

...

const app = new Vue({
    el: '#app',
    components: { App },
    router,
    store //<<<<<<<<
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.