home
home
home
home
一是全局设置
export default new Router({
linkActiveClass:'is-active', //全局设置样式的类名
routes: [
{
path: '/home',
name: 'home',
component: Home
},
]
})
二是局部设置
about
export default new Router({
mode:'history',
linkActiveClass:'is-active',
routes: [
{
path: '/home',
name: 'home',
component: home
},
{
path: '/document',
name: 'document',
component: document
},
{
path: '/about',
name: 'about',
component: about
},
{
path:'*', //当跳转的是其他路径时,重定向到指定的路径
//重定向路由有以下几种方式
// redirect:Home
// redirect:{path:'/home'}
// redirect:{name:'home'}
redirect:(to)=>{ //动态设置重定向的目标
//目标路由对象,就是访问路径的路由信息
if(to.path==='/123'){
return '/home'
}else if(to.path==='/456'){
return {path:'/document'}
}else{
return {name:'about'}
}
}
}
]
})
home
export default new Router({
mode:'history',
linkActiveClass:'is-active',
routes: [
{
path: '/',
name: 'home',
component: Home
},
{
path: '/document',
name: 'document',
component: document
},
{
path: '/about',
name: 'about',
component: about,
children:[
{
path:'', //默认子路由 /about
component:study
},
{
path:'work',
component:work
},
{
path:'hobby',
component:hobby
}
]
},
}
]
})
我是about
export default new Router({
mode:'history',
linkActiveClass:'is-active',
routes: [
{
path: '/',
name: 'home',
component: Home
},
{
path: '/document',
name: 'document',
components: {
default:document,
slider:slider
}
},
}
]
})
第一个是使用坐标记录滚动条的位置
export default new Router({
mode:'history',
linkActiveClass:'is-active',
scrollBehavior(to,from,savePosition){
console.log(to) //要进入的目标路由对象 要去向哪里
console.log(from) //要离开的路由对象 从哪里来
console.log(savePosition) //记录滚动条的坐标,点击前进后退时记录值
//当有滚动行为时,记录滚动条的坐标
if(savePosition){
return savePosition
}else{
return {x:0,y:0}
}
},
第二个是使用hash值定位元素
//子组件
我是document
定位到这个元素
//父组件
document
//路由配置
export default new Router({
mode:'history',
linkActiveClass:'is-active',
scrollBehavior(to,from,savePosition){
console.log(to) //要进入的目标路由对象 要去向哪里
console.log(from) //要离开的路由对象 从哪里来
console.log(savePosition) //记录滚动条的坐标,点击前进后退时记录值
//当有滚动行为时,记录滚动条的坐标
/* if(savePosition){
return savePosition
}else{
return {x:0,y:0}
}*/
if(to.hash){
return {
selector:to.hash
}
}
},
})
这次就知道这里,下次继续总结