Skip to content

路由守卫

约 124 字小于 1 分钟

前端

2025-03-04

本文作者:程序员飞云

本站地址:https://www.flycode.icu

使用场景,vue里面判断用户是否携带了token

创建js脚本

import router from "@/router/index";
import store from "@/store";
router.beforeEach((to, from, next) => {
    // 白名单,放行
    const whiteList = ["/login"];
    // 获取token
    const token = store.getters.GET_TOKEN;
    // 存在token,放行
    if (token) {
        next();
    } else {
        // 路径存在白名单,放行
        if (whiteList.includes(to.path)) {
            next();
        } else {
            // 否则跳转主页
            next("/login");
        }
    }
});

引入到main.ts

import '@/router/permission'

贡献者

  • flycodeuflycodeu

公告板

2025-03-04正式迁移知识库到此项目