2016-07-17 97 views
0

我遇到了vue問題。Vue路由器對象爲空

我的登錄看起來是這樣的:

import Vue from 'vue'; 
import {router} from '../main'; 

export default { 

    user: { 
    authenticated: false, 
    id: 0, 
    name: '', 
    email: '' 
    }, 

    login (context, creds) { 
    Vue.http.post('/api/login', creds) 
     .then(({data: {token}}) => { 
     // Hide the error message in case this time 
     // the creds were valid. 
     context.hideError(); 

     this.setToken(token); 
     this.getUserInfo(token); 

     router.go({ path: '/home' }); 
     }, (error) => { 
     // Show some error message on the invoking vm 
     // telling the user that his/her credentials 
     // are wrong. 
     context.showError(); 
     }); 
    }, 

..... 

但由於某些原因我router objectnull。我收到的錯誤:Cannot read property 'go' of undefined(…)

我從main.js

當我做輸入路由器的路由器是這樣的:

var router = new Router({ 
    history: true 
}); 

請告訴我,我在做什麼錯在這裏!

+0

你需要在main.js中導出你的路由器! –

+0

@IsmailRBOUH謝謝:) – Jamie

+0

請添加一個答案並將其標記爲已接受(以便將來人們面對同樣的問題將能夠輕鬆發現)。 –

回答

2

我有這樣的:

var router = new Router({ 
    history: true 
}); 

取而代之的是:

export var router = new Router({ 
    history: true 
}); 

@Ismail RBOUH感謝您的幫助。