2016-07-17 66 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感谢您的帮助。