路由导航守卫

位置:src/permission.js

1.

import router from "./router";
import { useUserStore } from "@/store/userStore";

// 进度条
import NProgress from "nprogress";
import "nprogress/nprogress.css";
//自定义进度样式
NProgress.configure({
  easing: "ease", // 动画方式
  speed: 1500, // 递增进度条的速度
  showSpinner: true, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3, // 初始化时的最小百分比
});
router.beforeEach(async (to, from, next) => {
  NProgress.start();
  //随机增加进度
  NProgress.inc();
  let userStore = useUserStore();
  if (userStore?.userInfo?.token) {
    if (to.path == "/login") {
      next(from.path);
    } else {
      next();
    }
  } else {
    next();
  }
});

router.afterEach(() => {
  //关闭掉进度条
  NProgress.done();
});

2.  在main.js引入

import "@/permission";

你可能感兴趣的:(vue.js)