Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > js框架/js库

vue路由拦截及页面跳转的设置方法

来源:中文源码网    浏览:148 次    日期:2024-03-27 20:57:31
【下载文档:  vue路由拦截及页面跳转的设置方法.txt 】


vue路由拦截及页面跳转的设置方法
路由设置:router/index.js
export default new Router({
routes: [
{
path: '/selfcenter',
name: 'selfcenter',
meta: {
requireAuth: true // 配置此条,进入页面前判断是否需要登陆
},
component: selfcenter
}
]
})
main.js:
/* eslint-disable no-new */
router.beforeEach((to, from, next) => {
if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
if (sessionStorage.getItem('sid')) { // 查询本地存储信息是否已经登陆
next();
} else {
next({
path: '/login', // 未登录则跳转至login页面
query: {redirect: to.fullPath} // 登陆成功后回到当前页面,这里传值给login页面,to.fullPath为当前点击的页面
});
}
} else {
next();
}
});
login.vue:
登陆成功后:
sessionStorage.setItem('sid', res.data.data.sid); // 设置本地存储信息
this.$router.push(this.$route.query.redirect); // 跳转至前一页,this.$route.query.redirect是获取上面传递过来的值
总结
以上所述是小编给大家介绍的vue路由拦截及页面跳转的设置方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对中文源码网网站的支持!

相关内容