router vue 多个路径_vue-router,子路由,和同级多个子路由

本文介绍了Vue的路由配置,包括最顶层的router - view配置,当路径为/时显示HelloWorld组件;子路由配置,路径为/a或/b时显示对应组件;以及同级多个子路由配置,路径为/child_a或/child_b时,不同router - view显示不同组件。

首先App.vue有一个最顶层的router-view

//App.vue

export default {

name: 'App'

}

router/index.js文件配置的意思是,当浏览器路径为/,App.vue里的router-view显示HelloWorld组件

//router/index.js

const router = new Router({

routes: [

{

path: '/',

name: 'HelloWorld',

component:HelloWorld

}

]

})

子路由,child.vue有一个子路由router-view,再创建两个组件a.vue和b.vue

//child.vue

export default {

name: 'child'

}

router/index.js文件配置的意思是,

当浏览器路径为/a,child.vue里的子路由router-view显示a组件

当浏览器路径为/b,child.vue里的子路由router-view显示b组件

默认显示a组件

//router/index.js

const router = new Router({

routes: [

{

path: '/',

name: 'HelloWorld',

component:HelloWorld

},

{

path: '/a',//路径配置和第一个children一样,表示子路由默认显示a.vue

name: 'a',

component: child,

//path: '/child',

//redirect:'/a',//或者也可以重定向到/a

children:[

{

path:'/a',

name: 'a',

component: a

},

{

path:'/b',

name: 'b',

component: b

}

]

},

]

})

同级多个子路由,child2.vue有3个子路由router-view,再创建两个组件child_a.vue,child_b.vue,child_c.vue

//child2.vue

export default {

name: 'child2'

}

router/index.js文件配置的意思是,

当路径为/child_a时,默认router-view显示child_a,name为b的router-view显示child_b,name为c的router-view显示child_c

当路径为/child_b时,默认router-view显示child_c,name为b的router-view显示child_b,name为c的router-view显示child_a

默认路径为/child_a

//router/index.js

const router = new Router({

routes: [

{

path: '/',

name: 'HelloWorld',

component:HelloWorld

},

{

path: '/a',

name: 'a',

...//同上,省略

},

{

path: '/child_a', //路径配置和第一个children一样,表示默认路径为/child_a

name: 'child_a',

component: child2,

//path: '/child2',

//redirect:'/child_a', //或者也可以重定向到/child_a

children:[ //此处要配置在children里也是因为3个路由都为child2里的子路由

{

path: '/child_a', //当路径为/child_a时

components: { //这里注意是components,加s

default: child_a, //默认router-view显示child_a

b: child_b, //name为b的router-view显示child_b

c: child_c //name为c的router-view显示child_c

}

},

{

path: '/child_b',//当路径为/child_b时

components: {

default: child_c,//默认router-view显示child_c

b: child_b, //name为b的router-view显示child_b

c: child_a //name为c的router-view显示child_a

}

}

]

},

]

})

标签:vue,name,child,router,path,路由,view

来源: https://blog.csdn.net/KenThomas/article/details/90545928

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值