• 从零开始学VUE之VueRouter(导航守卫)


    导航守卫

    需要先定义路由,然后通过路由对象调用

    import Vue from 'vue'
    // 导入路由
    import Router from 'vue-router'
    import HelloWorld from '@/components/HelloWorld'
    
    // import home from '../components/Home'
    // import about from '../components/About'
    // import user from '../components/User'
    
    const home = () => import('../components/Home')
    const homeNews = () => import('../components/HomeNews')
    const homeMessage = () => import('../components/HomeMessage')
    
    const about = () => import('../components/About')
    const user = () => import('../components/User')
    const profile = () => import('../components/Profile')
    
    // Vue加载
    Vue.use(Router)
    
    // 传入路由映射配置 导出路由实例
    const router = new Router({
      // 设置模式为 history
      mode: 'history',
      linkActiveClass: 'active',
      linkExactActiveClass: 'noactive',
      routes: [// 传递参数的占位符
        {
          // 在默认的情况下 重定向到主页
          path: '',
          redirect: "/home"
        }, {
    
          path: '/user/:userId',
          name: 'user',
          meta:{ title: '用户' },
          component: user
        }, {
          path: '/home',
          name: 'home',
          meta:{ title: '首页' },
          component: home,
          // 嵌套路由
          children: [
            {
              path: 'news',
              meta:{ title: '首页|新闻' },
              component: homeNews
            }, {
              path: 'message',
              meta:{ title: '首页|消息' },
              component: homeMessage
            }, {
              path: '',
              redirect: 'news'
            }
          ]
        }, {
          path: '/about',
          name: 'about',
          meta:{ title: '关于' },
          component: about
        }, {
          // 传递参数
          path:'/profile',
          meta:{ title: '档案' },
          component:profile
        }
      ]
    })
    // 前置守卫函数
    router.beforeEach((to, from, next) => {
      // 路由导航守卫
      // 在路由跳转调用之前
      // from 从哪里 to 到哪里
      // document.title = to.matched[0].meta.title;
      document.title = to.meta.title;
      // 调用next放行路由
      next();
    })
    // 后置钩子函数
    router.afterEach((to,from) => {
    
    })
    
    export default router

    进阶:https://router.vuejs.org/zh/guide/advanced/navigation-guards.html#%E7%BB%84%E4%BB%B6%E5%86%85%E7%9A%84%E5%AE%88%E5%8D%AB

    作者:彼岸舞

    时间:2021628

    内容关于:VUE

    本文属于作者原创,未经允许,禁止转发

  • 相关阅读:
    URAL1204. Idempotents(扩展欧几里得)
    URAL1049. Brave Balloonists
    URAL1133. Fibonacci Sequence(二分)
    URAL1352. Mersenne Primes
    URAL1118. Nontrivial Numbers
    hdu3270Arranging Your Team(dfs)
    Codeforces Round #209 (Div. 2)C
    KMP
    树状数组求逆序对
    poj2352
  • 原文地址:https://www.cnblogs.com/flower-dance/p/14944598.html
Copyright © 2020-2023  润新知