zoukankan      html  css  js  c++  java
  • yb课堂 实战之路由拦截和订单模块 《四十二》

    前置守护 路由拦截功能开发

    文档:https://router.vuejs.org/zh/guide/advanced/navigation-guards.html

    router里面配置需要登陆的路由

    meta: { requiresAuth:true }

    import Vue from 'vue'
    import VueRouter from 'vue-router'
    import Home from '../views/Home/Home.vue'
    import CourseDetail from '../views/CourseDetail/CourseDetail.vue'
    import Login from '../views/Login/Login.vue'
    import Order from '../views/Order/Order.vue'
    import Pay from '../views/Pay/Pay.vue'
    import Personal from '../views/Personal/Personal.vue'
    import Register from '../views/Register/Register.vue'
    
    Vue.use(VueRouter)
    
    const routes = [
      {
        path: "/",
        name: "Home",
        component: Home
      },
      {
        path: "/coursedetail",
        name: "CourseDetail",
        //按需加载
        component: ()=>import("../views/CourseDetail/CourseDetail")
        //component: CourseDetail
      },
      {
        path: "/login",
        name: "Login",
        component: Login
      },
      {
        path: "/order",
        name: "Order",
        component: Order,
        meta: { requiresAuth:true }
      },
      {
        path: "/pay",
        name: "Pay",
        component: Pay,
        meta: { requiresAuth:true }
      },
      {
        path: "/personal",
        name: "Personal",
        component: Personal,
        meta: { requiresAuth:true }
      },
      {
        path: "/register",
        name: "Register",
        component: Register
      }
    ]
    
    const router = new VueRouter({
      routes
    })
    
    export default router

    main.js里面配置路由拦截

    //拦截拦截,拦截全部路由,每次操作路由都是被拦截进行判断
    router.beforeEach((to, from, next) => {
      const token = localStorage.getItem('token');
      //筛选需要传token的路由,匹配route里面需要登陆的路由,如果匹配到就是true
      if (to.matched.some(record => record.meta.requiresAuth)) {
        //根据token是否有,帕努单是否需要调到登陆页面
        if (token) {
          next();
        } else {
          next({ path: '/login' })
        }
      }else
      {
        next();
      }
    })

    作者:陈彦斌

    个性签名:没有学不会的技术,只有不学习的人!
    联系方式:543210188(WeChat/QQ),推荐WeChat
  • 相关阅读:
    window.top.location
    JS 退出系统并跳转到登录界面的实现代码
    CSS position 属性
    在网页中插入时间 自动更新
    CKeditor 配置使用
    使用JspSmart文件上传
    复制的web工程为什么不能部署到tomcat
    JDBC 4 PreparedStatement 与Statement 的区别
    每周会商自动化
    晚上的亲子时光
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/13355219.html
Copyright © 2011-2022 走看看