优化 router,支持 isAuth,并添加示例
This commit is contained in:
@@ -1,8 +1,17 @@
|
||||
import { router, useStore } from "@/cool";
|
||||
|
||||
router.beforeEach((to, _, next) => {
|
||||
/**
|
||||
* 路由跳转前的全局钩子(如修改 pages.json 后需重新编译项目以确保路由信息生效)
|
||||
* @param to 跳转页
|
||||
* @param from 当前页
|
||||
* @param next 跳转函数
|
||||
*/
|
||||
router.beforeEach((to, from, next) => {
|
||||
const { user } = useStore();
|
||||
if (to.meta?.isAuth == true) {
|
||||
|
||||
// 判断是否需要登录
|
||||
if (to.isAuth == true || to.meta?.isAuth == true) {
|
||||
// 如果用户信息为空,则跳转到登录页
|
||||
if (!user.isNull()) {
|
||||
next();
|
||||
} else {
|
||||
@@ -11,4 +20,4 @@ router.beforeEach((to, _, next) => {
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user