vue路由步骤

路由简单的理解就是—映射

1.步骤

在这里插入图片描述

2.原始代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Vue路由机制</title>
	<body>
		<div id="app">
			<!-- 2.定义路由链接
				router-link 解析之后,变为a标签.
				to 解析之后, 变为href属性
			 -->
			<router-link to="/user">用户</router-link>
			<router-link to="/dog">狗狗</router-link>
			
			<!-- 3.定义路由填充位 该位置用来展现具体页面(组件) -->
			<router-view></router-view>
			
			
		</div>
		
		<!-- 4.2 定义组件模版 -->
		<template id="userTem">
			<div>
				<h1>我是组件的内容!!!!!</h1>
			</div>
		</template>
		
		<template id="dogTem">
			<div>
				<h1>养只宠物狗</h1>
			</div>
		</template>
		
		
		
		
		<!-- 1.引入vue.js -->
		<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
		<!-- 1.导入路由JS 注意顺序-->
		<script src="../js/vue-router.js"></script>
		<script>
			//4.定义组件
			let user = {
				//4.1定义页面
				template: "#userTem"
			}
			
			let dog = {
				template: "#dogTem"
			}
			
			
			
			//5.创建路由对象,定义路由规则
			let router = new VueRouter({
				//定义路由规则
				routes: [
						/* 
							利用redirect实现路由重定向
						 */
						{path: "/", redirect: "/dog"},
						{path: "/user", component: user},
						{path: "/dog", component: dog}
				]
			})
			
			//6.将路由对象交给Vue对象管理
			const vue = new Vue({
				el: "#app",
				//绑定路由
				//router: router  名称一致时,可以简化
				router
			})
			
		</script>
	</body>
</html>

3.路由嵌代码

要点:映射中的children属性,写嵌套的映射。

{path: “/dog”, component: dog, children: [
{path: “/dog/samo”, component: samo},
{path: “/dog/hsq”, component: hsq}
]}

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Vue路由机制</title>
	<body>
		<div id="app">
			<!-- 2.定义路由链接
				router-link 解析之后,变为a标签.
				to 解析之后, 变为href属性
			 -->
			<router-link to="/user">用户</router-link>
			<router-link to="/dog">狗狗</router-link>
			
			<!-- 3.定义路由填充位 该位置用来展现具体页面(组件) -->
			<router-view></router-view>
			
			
		</div>
		
		<!-- 4.2 定义组件模版 -->
		<template id="userTem">
			<div>
				<h1>我是组件的内容!!!!!</h1>
			</div>
		</template>
		
		<template id="dogTem">
			<div>
				<h1>养只宠物狗</h1>
				<!-- 定义子组件路由 -->
				<router-link to="/dog/samo">萨摩耶</router-link>
				<router-link to="/dog/hsq">哈士奇</router-link>
				<!-- 定义子组件占位符 -->
				<router-view></router-view>
			</div>
		</template>
		
		<!-- 1.引入vue.js -->
		<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
		<!-- 1.导入路由JS 注意顺序-->
		<script src="../js/vue-router.js"></script>
		<script>
			//4.定义组件
			let user = {
				//4.1定义页面
				template: "#userTem"
			}
			
			let dog = {
				template: "#dogTem"
			}
			
			let samo = {
				template: `<h1>通体雪白的神兽</h1>`
			}
			
			let hsq = {
				template: `<h1>拆家狂魔</h1>`
			}
			
			
			
			
			
			//5.创建路由对象,定义路由规则
			let router = new VueRouter({
				//定义路由规则
				routes: [
						/* 
							利用redirect实现路由重定向
						 */
						{path: "/", redirect: "/dog"},
						{path: "/user", component: user},
						{path: "/dog", component: dog, children: [
							{path: "/dog/samo", component: samo},
							{path: "/dog/hsq", component: hsq}
						]},
						/*该请求是/目录下的请求,所以组件渲染也在根组件 */
						/* {path: "/dog/samo", component: samo},
						{path: "/dog/hsq", component: hsq}, */
						
				]
			})
			
			//6.将路由对象交给Vue对象管理
			const vue = new Vue({
				el: "#app",
				//绑定路由
				//router: router  名称一致时,可以简化
				router
			})
			
		</script>
	</body>
</html>

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值