VueJs:无法使用router.push

时间:2019-06-26 10:05:49

标签: vue.js vuejs2 vuex vue-router

因此,我想对store.js上的router.push进行检查,但我不断收到错误Cannot read property 'push' of undefined

我尝试将vue-router导入到store.js中,但还是徒劳

这是我的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,
});

这是我的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);
                })
        }
    }
})

如您所见,在我调用doLogin()函数并使用axios之后,它停止在this。$ router.push({path:'/'})行,从而导致错误。

1 个答案:

答案 0 :(得分:1)

您需要制作一个router.js文件

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

Vue.use(Router)

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

export default router

app.js中,将vue-router的导入替换为新的router.js,然后删除Vue.use(Router)

在商店中,this不是Vue实例。 将router.js导入您的store.js

import router from './router'

然后您可以像这样访问它;

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

我还注意到您尚未将商店添加到Vue实例。导入并添加app.js

import store from './store'

...

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