Vue-18-嵌套路由(子路由)

一、知识点部分

嵌套路由的写法:

  1. 路由中包含的一个属性:children
var router = new VueRouter({
		routes:[
		{	
			path:'/login',
			name:'login',
			component:Login,
			children:[{}]
			}
		]
})
  1. children和routers的格式是一样的,需要配置path等属性。(注意:在路由配置中,以 / 开头的嵌套路径会被当作根路径。 这让你充分的使用嵌套组件而无须设置嵌套的路径。所以子路由不需要在路径前写“/”)
二、代码部分
		var ComponentA = {
			template:`
			<div>
				<h3>Let's learn Vue Togeter!</h3>
				<router-link :to="{name:'myA'}">Login A</router-link>
				<div class="boxa">
					<router-view></router-view>
				</div>
			</div>
			`
		};
		var ComponentB = {
			template:`
			<div>
				<h3>You are the best!</h3>
					<router-link :to="{name:'myB'}">Login B</router-link>
				<div class="boxb">
					<router-view></router-view>
				</div>
			</div>
			
			`
		};
		var MyA = {
			template:`
				<div>
					<h3>A little component A</h3>	
				</div>
			`
		};
		var MyB = {
			template:`
				<div>
					<h3>A little component B</h3>	
				</div>
			`
		};
		var App = {
			template:`
				<div>
					<h3>Hello Vue!</h3>
					<router-link :to="{name:'componentA'}">切换A组件</router-link>
					<router-link :to="{name:'componentB'}">切换B组件</router-link>
					<router-view></router-view>
				</div>
			`
		};
		var router  = new VueRouter({
			routes:[
				{
					path:"/componentA",
					component:ComponentA,
					name:"componentA",
					children:[
						{
							path:'myA',
							component:MyA,
							name:"myA",
							children:[
								{
									path:"myA-A",
									component:MyA-A,
									name:"Myaa"
								}
							]
						}
					]
				},
				{
					path:"/componentB",
					component:ComponentB,
					name:"componentB",
					children:[
						{
							path:'myB',
							component:MyB,
							name:"myB"
						}
					]
				}
			]
		})
		new Vue({
			el:"#app",
			data(){
				return{

				}
			},
			template:`
				<App/>
			`,
			components:{
				App
			},
			router
		})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值