ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-07-02 10:52:00  阅读:163  来源: 互联网

标签:VUE VueRouter .. title 从零开始 meta components import 路由


从零开始学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
 

标签:VUE,VueRouter,..,title,从零开始,meta,components,import,路由
来源: https://blog.51cto.com/u_15196075/2967787

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有