zoukankan      html  css  js  c++  java
  • vue导航守卫

    三部分

    1. router(VueRouter实例)守卫 -- 全局路由守卫
    2. router守卫 -- 路由守卫
    3. component守卫 -- 组件守卫

    • const router = new Router({})
    • router.beforeEach(function (to,from,next) {} //
    • export default router
        router.beforeEach(function (to,from,next) {
          // console.log(to,from)
          if(to.name == 'blog') {
            if(to.matched[0].meta.is_login) {
              next()
            }else{
              console.log("login")
              next({name: 'login'})
            }
          }else if(to.name == 'login') {
            if(to.matched[0].meta.is_login) {
              next({name: from.name})
              console.log(from)
            }else {
              next()
            }
          }else {
            next()
          }
        })
        <template>
            <button @click='login'>LOGIN</button>
        </template>
        <script>
        export default {
            created() {
                // console.log(this.$route)
            },
            methods: {
                login() {
                    // console.log(this.$route)
                    this.$route.matched[0].meta.is_login = true;  //
                    this.$router.push({name: 'blog'})  //
                }
            }
        }
        </script>

    Vue.use(Router)
    
    const router =  new Router({
      routes: [
        {
          path: '/login',
          name: 'login',
          component: Login,
          meta: {
            index: 3,
            is_login: true
          },
          beforeEnter(to,from,next) {
            // console.log(to,from)
            if(to.meta.is_login) {
              next({name:from.name})
            }else{
              next()
            }
          }
        }
      ]
    })
    
    router.beforeEach(function (to,from,next) {
      // console.log(to)
      if(to.name == 'blog') {
        if(to.matched[0].meta.is_login) {
          next()
        }else{
          // console.log("login")
          next({name: 'login'})
        }
      }else if(to.name == 'login') {
        if(to.matched[0].meta.is_login) {
          next({name: from.name})
          // console.log(from)
        }else {
          next()
        }
      }else {
        next()
      }
    })
    
    export default router

    3.

    未完待续


    html&css
  • 相关阅读:
    jupyter notebook 和 opencv
    AMD 处理器 Ubuntu 16.04 LTS 配置 opencv、caffe 小结
    caffe 在 windows 使用
    MATLAB数字图像处理
    游记(5)
    POJ-1789-Truck History 解题报告
    POJ-2560-Freckles 解题报告
    POJ-1308-Is It A Tree? 解题报告
    POJ-1182-食物链 解题报告
    POJ-1861-Network 解题报告
  • 原文地址:https://www.cnblogs.com/goff-mi/p/9392402.html
Copyright © 2011-2022 走看看