需要先定义路由,然后通过路由对象调用
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
作者:彼岸舞
时间:2021\06\28
内容关于:VUE
本文属于作者原创,未经允许,禁止转发