Vue.js 登录后重定向
伙计们。有人能帮我解决这个重定向问题吗? 我有这个路由器文件:Vue.js 登录后重定向,vue.js,Vue.js,伙计们。有人能帮我解决这个重定向问题吗? 我有这个路由器文件: router.beforeEach((to, from, next) => { if (to.matched.some(record => record.meta.requiresAuth)) { if (!store.state.auth.is_authenticated) { next({ path: '/login', query: { r
router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.requiresAuth)) {
if (!store.state.auth.is_authenticated) {
next({
path: '/login',
query: {
redirect: to.fullPath
}
})
} else {
next()
}
} else if (to.matched.some(record => record.meta.requiresGuest)) {
if (store.state.auth.is_authenticated) {
next({
path: '/',
query: {
redirect: to.fullPath
}
})
} else {
next()
}
} else {
next()
}
})
当前身份验证状态存储在vuex状态。当我登录时,状态正在改变,但页面没有任何变化,只有标题组件更新
<script>
import { mapGetters, mapActions } from "vuex";
export default {
name: "Login",
data: () => {
return {
email: "",
password: ""
};
},
computed: mapGetters(["currentUser"]),
methods: {
...mapActions(["loginUser"]),
login: function(event) {
event.preventDefault();
this.loginUser({ email: this.email, password: this.password });
}
},
updated() {
if (this.currentUser.is_authenticated) {
this.$router.go({ path: this.$router.path });
}
}
};
</script>
从“vuex”导入{MapGetter,mapActions};
导出默认值{
名称:“登录”,
数据:()=>{
返回{
电邮:“,
密码:“
};
},
计算:mapGetters([“currentUser”]),
方法:{
…映射操作([“登录用户”]),
登录:函数(事件){
event.preventDefault();
this.login用户({email:this.email,密码:this.password});
}
},
更新的(){
if(此.currentUser.is_已验证){
this.$router.go({path:this.$router.path});
}
}
};
如果store.state.auth.is\u authenticated为true,则意味着您必须在该标志变为true后登录,如下图所示。$router.go();如果您在没有任何参数的情况下进行此操作,这意味着您将进入与您完全相同的页面(登录页面),但随后它将由您的警卫触发,并将您重定向到面板页面,如果store.state.auth.is_authenticated为true,则意味着您必须在该标志变为true后登录,类似这样的操作。$router.go();如果您在没有任何参数的情况下进行此操作,这意味着您将进入与您完全相同的页面(登录页面),但随后它将由您的警卫触发,并将您重定向到面板页面或其他页面,请像这样修改您的代码并检查
helper.js
export function initialize(store, router)
{ router.beforeEach((to,from,next)=>{
const requiresAuth= to.matched.some(record=>record.meta.requiresAuth)
const currentUser = store.state.auth.user;
if(requiresAuth && !currentUser){ next('/login');}
else if(to.path=='/login' && currentUser){ next('/')}
//if user already authenticated and user tries to open login page
//redirect to / path , it can be modified as per config
else { next()}
})
}
将此添加到main.js中
import {initialize} from './helper';
initialize(store, router);
在您的登录页面中,更新的部分不需要作为router.before。helper.js中的每一个代码都可以处理这个问题
methods: {
...mapActions(["loginUser"]),
login: function(event) {
event.preventDefault();
this.loginUser({ email: this.email, password: this.password })
.then(()=>{ this.$router.replace({name:'dashboard'})
//forward to this path after login
}).catch(()=>{console.log('login fail') });
}
},
请像这样修改您的代码并检查 helper.js
export function initialize(store, router)
{ router.beforeEach((to,from,next)=>{
const requiresAuth= to.matched.some(record=>record.meta.requiresAuth)
const currentUser = store.state.auth.user;
if(requiresAuth && !currentUser){ next('/login');}
else if(to.path=='/login' && currentUser){ next('/')}
//if user already authenticated and user tries to open login page
//redirect to / path , it can be modified as per config
else { next()}
})
}
将此添加到main.js中
import {initialize} from './helper';
initialize(store, router);
在您的登录页面中,更新的部分不需要作为router.before。helper.js中的每一个代码都可以处理这个问题
methods: {
...mapActions(["loginUser"]),
login: function(event) {
event.preventDefault();
this.loginUser({ email: this.email, password: this.password })
.then(()=>{ this.$router.replace({name:'dashboard'})
//forward to this path after login
}).catch(()=>{console.log('login fail') });
}
},