vue相关问题

vue路由元信息

2019-04-24  本文已影响0人  一条小团团ovo的就很棒

$route.matched:一个路由匹配到所有的路由记录

import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
export default new VueRouter({
    mode:"history",
    routes:[
        {
            path:"/",
            redirect:"/login"
        },
        {
            path:"/home",
            component:resolve=>require(["../components/home.vue"],resolve),
            children:[
                  {
                      path:"/about",
                      component:resolve=>require(["../components/about.vue"],resolve),
                      meta:{requireLogin:true}
                  } 
            ]
        },
        {
            path:"/login",
            component:resolve=>require(["../components/login.vue"],resolve)
        }
    ]
})

VueRouter.beforeEach((to,from,next)=>{
        if(to.matched.some(record=>record.meta.requireLogin)){
      //to.matched表示匹配路由的所有路由记录的数组
      //record.meta.requireLogin表示路由记录中的meta中的requireLogin属性
           let token=localStorage.getItem("token"); 
           if(token==null||token==""){
                  next("/login")
            }else{
                  next()
            }
        }else{
              next();
        }
 })
上一篇下一篇

猜你喜欢

热点阅读