【发布时间】:2018-11-10 18:38:28
【问题描述】:
我正在登录,但在登录方法成功后,我正在为 $router 对象推送路由,但它不起作用,有人可以帮帮我吗?
我的代码:
doLogin(){
this.attemptLogin({...this.user}).then(function(){
this.$router.push('/')
} )
},
因此,登录方法按预期执行,但回调 this.$router.push('/') 没有运行。
我的attemptLogin 是一个动作,它的代码如下:
export const attemptLogin = ( {commit}, user) => {
return http.post('login', {
'email': user.email,
'password': user.password
}).then(response => {
return commit(types.setToken, response.data)
}).catch( e => console.log(e))
}
【问题讨论】:
-
你需要从
http.post返回promise。所以应该是return http.post(...)。 -
@Bert 我做到了,但没有任何反应。
-
这里的
this是否存在范围问题?我不记得作用域是如何与() => {}语法一起工作的。在function() {}语法中,你必须.bind(this)到函数。 -
() => {}将this绑定到父作用域,所以你之前已经正确了 -
问题已解决,我已将
this.$router.push('/')方法更改为this.$route.go('/')并且它有效。谢谢你帮助我。
标签: javascript vue.js vue-router