1. 路由组件传参
页面组件能够经过props接收url参数
布尔模式
{html
path: '/argu/:name', name: 'argu', component: () => import('@/views/argu.vue'), props: true
}
对象模式
{vue
path: '/about', name: 'about', component: () => import('@/views/About.vue'), props: { food: 'banana' }
}
函数模式
{vue-router
path: '/search', component: SearchUser, props: (route) => ({ query: route.query.q })
}浏览器
2. HTML5 History模式
vue-router 默认 hash 模式 —— 使用 URL 的 hash 来模拟一个完整的 URL,例: loccalhost:8080/#/,#就是hash模式app
若是不想要很丑的 hash,咱们能够用路由的 history 模式,这种模式充分利用 history.pushState API 来完成 URL 跳转而无须从新加载页面。less
const router = new VueRouter({
mode: 'history',
routes: [...]
})异步
当你使用 history 模式时,URL 就像正常的 url,例如 http://yoursite.com/user/id,也好看!
不过这种模式要玩好,还须要后台配置支持。由于咱们的应用是个单页客户端应用,若是后台没有正确的配置,当用户在浏览器直接访问 http://oursite.com/user/id 就会返回 404,这就很差看了。函数
因此呢,你要在服务端增长一个覆盖全部状况的候选资源:若是 URL 匹配不到任何静态资源,则应该返回同一个 index.html 页面,这个页面就是你 app 依赖的页面。ui
或者url
在 Vue 应用里面覆盖全部的路由状况,而后在给出一个 404 页面。
const router = new VueRouter({
mode: 'history',
routes: [
{ path: '*', component: NotFoundComponent }
]
})
3. 导航守卫
1.导航被触发。
2.在失活的组件里调用离开守卫。
3.调用全局的 beforeEach 守卫。
4.在重用的组件里调用 beforeRouteUpdate 守卫 (2.2+)。
5.在路由配置里调用 beforeEnter。
6.解析异步路由组件。
7.在被激活的组件里调用 beforeRouteEnter。
8.调用全局的 beforeResolve 守卫 (2.5+)。
9.导航被确认。
10.调用全局的 afterEach 钩子。
11.触发 DOM 更新。
12.用建立好的实例调用 beforeRouteEnter 守卫中传给 next 的回调函数。
4. 路由元信息
定义路由的时候能够配置 meta 字段
const router = new VueRouter({ routes: [ { path: '/foo', component: Foo, children: [ { path: 'bar', component: Bar, // a meta field meta: { requiresAuth: true } } ] } ] })
5. 过渡效果
<template> <div id="app"> <transition-group name="router"> <router-view key="default"/> <router-view key="email" name="email"/> <router-view key="tel" name="tel"/> </transition-group> </div> </template> <style lang="less"> .router-enter{ opacity: 0; } .router-enter-active{ transition: opacity 1s ease; } .router-enter-to{ opacity: 1; } .router-leave{ opacity: 1; } .router-leave-active{ transition: opacity 1s ease; } .router-leave-to{ opacity: 0; } </style>