是vue.js2代码,当用户尝试使用凭据登录时,它应该重定向到仪表板视图,但是即使提供了正确的凭据也不会重定向: 并说不能跳到同一页面。
if (username && password) {
const payload = { username, password };
this.$store.dispatch(LOGIN,payload)
.then((resp)=>{
if(resp && resp.data.message==="Welcome" ) {
this.error=false;
console.log(this.$route.query) // here it returns {} object
this.$router.push(this.$route.query.redirect || "/");
}
})
.catch(err => {
this.error=true;
console.log(err);
});
编辑操作登录
[LOGIN]({commit}, payload) {
// LOGIN user
return loginService.LoginUser(payload).then(res => {
const token = res.data.token;
const user = res.data.userName
localStorage.setItem('user', token)
localStorage.setItem('token',user)
// axios.defaults.headers.common['Authorization'] = token
commit(AUTH_SUCCESS, token,user);
return res;
}).catch(error => {
commit(AUTH_ERROR, error);
localStorage.removeItem('token')
localStorage.setItem('token',user)
});
},
// EDIT..ERROR
host-report-errors.js?44de:6 Unhandled promise rejection NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated", message: "Navigating to current location ("/pages/login") is not allowed", stack: "Error↵ at new NavigationDuplicated (webpack-int…al:///./node_modules/vue/dist/vue.esm.js:1862:57)"}
答案 0 :(得分:0)
添加条件:
if (
this.$route.path !== '/' &&
this.$route.path !== this.$route.query.redirect
) {
this.$router.push(this.$route.query.redirect || "/")
}
如错误所示,您无法转到当前所在的路线。