1.起步
- 0.导入Vue和VueRouter,调用Vue.use(VueRouter)-->1.定义组件-->2.定义路由-->3.创建router实例-->4.创建和挂载根实例
const Foo = {template:'<div>foo</div>'}
const Bar = {template:'<div>bar</div>'}
const routes = [
{
path:'/foo',
component:Foo
},
{
path:'/bar',
component:Bar
}
]
const router = new VueRouter({
routes,
})
const app = new Vue({
router
}).$mount('#app')
<style>
.router-link-active{
color:teal
}
</style>
2.动态路由匹配
- 某种模式匹配到的所有路由,全部映射到同个组件
- 比如,对于所有ID不同的用户,都使用User组件来渲染
- 我们可以在路由路径中使用“动态路径参数” 达到此效果
- “路径参数”使用冒号标记,当匹配到一个路由时,参数值会被设置到this.$route.params,可以在每个组件内使用
- 注意: 当使用路由参数时,从/user/foo导航到/user/bar,原来的组件实例会被复用,意味着,组件的生命周期钩子不会被调用
const User = {
template:'<div>User</div>'
}
const router = new VueRouter({
routes = [
{
path:'/user/:id',
component:User
}
]
})
3.响应路由参数变化
- 复用组件时,相对路由参数的变化做出响应的话,可以使用watch检测$route对象。
- 或者使用beforeRouteUpdate导航守卫
watch:{
'$route'(to,from){
}
}
beforeRouteUpdate(to,from,next){
}
4.通配符路由(另外还有高级匹配模式,后期学习)
- 当使用通配符路由时,$route.params会自动添加一个名为pathMath参数,它包含URL通过通配符被匹配的部分
const router = new VueRouter({
{
path:'*'
},
{
path:'/user-*'
}
})
5.嵌套路由
<div id="app">
<router-view></router-view>
</div>
const User = {
template:`<div>
<h2>User {{$route.params.id}}</h2>
<router-view></router-view>
</div>`
}
const Home = {
template:`<div>Home</div>`
}
const Profile = {
template:`<div>Profile</div>`
}
const Posts = {
template:`<div>Posts</div>`
}
const router = new VueRouter({
routes:[
{
path:'/user/:id',
component:User,
children:[
{
path:'',
component:Home
},
{
path:'profile',
component:Profile
},
{
path:'posts',
component:Posts
}
]
}
]
})
const App = new Vue({
router
}).$mount("#app")
6.编程式导航
this.$router.push('home')
this.$router.push({path:'home'})
this.$router.push({name:'Home',params:{userId:123}})
this.$router.push({path:'home',query:{plan:'private'}})
const userId = '123'
this.$router.push({name:'user',params:{userId}})
this.$router.push({path:`/user/${userId}`})
this.$router.push({path:'/user',params:{userId}})
7. 命名路由
const router = new VueRouter({
routes:[
{
path:'/user/:userId',
name:'user',
component:User
}
]
})
声明式导航
<router-link :to="{name:'user',params:{userId}}"></router-link>
编程式导航
this.$router.push({name:'user',params:{userId}})
8.命名视图
- 一个页面2个视图,而不是嵌套展示(比如sidebar和main)
- 这个时候可以用命名视图
<div id="app">
<router-view class="viewOne"></router-view>
<router-view class="viewTwo" name="a"></router-view>
<router-view class="viewThree" name="b"></router-view>
</div>
const Foo = {
template:`<div>Foo</div>`
}
const Bar = {
template:`<div>bar</div>`
}
const Baz = {
template:`<div>baz</div>`
}
const router = new VueRouter({
routes:[
{
path:'/',
//注意这里是components
components:{
default:Foo,
a:Bar,
b:Baz
}
}
]
})
9.嵌套命名视图
const router = new VueRouter({
routes:[
{
path:'/setting',
component:Setting,
children:[
{
path:'email',
component:Email
},
{
path:'profile',
components:{
default:UserProfile,
helper:UserProfileView
}
}
]
}
]
})
10.重定向&别名
const router = new VueRouter({
routes:[
{path:'/a',redirect:'/b',alias:'/b'}
{path:'/a',redirect:{name:'B'}}
{
path:'/a',redirect:to=>{
return '/b'
return {name:'B'}
}
}
]
})
11. 路由组件传参
const User = {
props:['id'],
template:`<div>User{{id}}</div>
`
}
const router = new VueRouter({
routes:[
{
path:'/user/:id',
component:User,
props:true
}
]
})
13.权限管理,路由配置
import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
import home from './modules/home'
export const constantRoutes = [
{
path: '/login',
component: () => import('@/views/login/index'),
hidden: true
},
{
path: '/404',
component: () => import('@/views/error-page/404'),
hidden: true
},
{
path: '/401',
component: () => import('@/views/error-page/401'),
hidden: true
}
]
export const asyncRoutes = [
home,
customerRouter,
workSchedule,
customerService,
platformSheet,
{ path: '*', redirect: '/404', hidden: true }
]
const createRouter = () => new Router({
scrollBehavior: () => ({ y: 0 }),
routes: constantRoutes
})
const router = createRouter()
export default router;
export function resetRouter(){
const newRouter = createRouter();
router.matcher = newRouter.matcher
}