vue路由的使用

地址:

入门 | Vue Router

一、导入vuerouter依赖包

注意,一定要先引入vue,再引入vue-router(引入vue在引入vue-router的上面)。不然会报错

	<head>
		<meta charset="utf-8">
		<title></title>
		<!-- 引入vue -->
		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
		
		<script src="https://unpkg.com/vue-router@2.0.0/dist/vue-router.js" type="text/javascript"></script>
		

	</head>

 注意点:如果在html页面中使用vuerouter  在使用script引入的时候 一定要放在vue下面

二、vuerouter的基本使用

为啥使用路由:

通过单页面实现一个网站。多个不同的请求,展示的是同一个页面内的不同组件,但是本质是同一个页面。

实现路由的步骤:

第一步:定义展示页面内容的组件

		<script type="text/javascript">
			
			// 定义组件样式
			// 登录页面的组件
			const loginpage = {template:`<h1>-----首页--------</h1>`}
			// 首页组件
			const homepage = {template:`<h1>===登录页===</h1>`}
			
		</script>


第二步:定义路由匹配规则

 定义一些路由 ,每个路由都需要映射到一个组件。

			// 定义路由规则
			const routes = [
				{path:'/',component: homepage },
				{path:"/loginpage",component: loginpage},
			]


第三步:初始化路由对象,创建一个路由对象,创建路由实例并传递 `routes` 配置

			// 创建一个路由对象,创建路由实例并传递 `routes` 配置
			const router = new VueRouter({
				routes, // `routes: routes` 的缩写
			})


第四步:将路由对象绑定到vue实例中去

			var vm = new Vue({
				el: '#app',
				// 将路由对象绑定到vue实例中
				router

			});


第五步:在根组件中定义路由视图的展示位置

		<div id="app">
			
			<!-- <router-link to="/home">主页</router-link> -->
			<!-- <router-link to="/login">登录</router-link> -->
			<!-- 路由视图 -->
			<router-view></router-view>
		</div>

            

整体代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<!-- 引入vue -->
		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
		
		<script src="https://unpkg.com/vue-router@2.0.0/dist/vue-router.js" type="text/javascript"></script>
		
	</head>
	
	
	<body>
		
		<div id="app">
			<router-link to="/home">主页</router-link>
			<router-link to="/login">登录</router-link>
			<!-- 路由视图 -->
			<router-view></router-view>
			
		</div>
		
		
		
		<script type="text/javascript">
			
			// 定义组件样式
			// 登录页面的组件
			const homepage = {template:`<h1>-----首页--------</h1>`}
			// 首页组件
			const loginpage = {template:`<h1>===登录页===</h1>`}
			
			// 定义路由规则
			const routes = [
				{path:'/home',component: homepage },
				{path:'/login',component: loginpage},
			]
			
			// 创建一个路由对象,创建路由实例并传递 `routes` 配置
			const router = new VueRouter({
				routes, // `routes: routes` 的缩写
			})
			
			var vm = new Vue({
				el: '#app',
				// 将路由对象绑定到vue实例中
				router
			});
		</script>
		
		
		
	</body>
</html>

三、声明式路由&编程式路由

声明式

点击link之后,直接跳转路由

<router-link :to="...">

编程式

经过函数逻辑处理,函数里面调用跳转路由的方法

router.push(...)

3.1 举例:声明式路由使用

整体代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<!-- 引入vue -->
		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
		
		<script src="https://unpkg.com/vue-router@2.0.0/dist/vue-router.js" type="text/javascript"></script>
		
	</head>
	
	
	<body>
		
		<div id="app">
			<router-link to="/home">
				<button type="button">首页</button>
			</router-link>
			<router-link to="/login">
				<button type="button">登录</button>
			</router-link>
			<!-- 路由视图 -->
			<router-view></router-view>
			
		</div>
		
		
		
		<script type="text/javascript">
			
			// 定义组件样式
			// 登录页面的组件
			const homepage = {template:`<h1>-----首页--------</h1>`}
			// 首页组件
			const loginpage = {template:`<h1>===登录页===</h1>`}
			
			// 定义路由规则
			const routes = [
				{path:'/home',component: homepage },
				{path:'/login',component: loginpage},
			]
			
			// 创建一个路由对象,创建路由实例并传递 `routes` 配置
			const router = new VueRouter({
				routes, // `routes: routes` 的缩写
			})
			
			var vm = new Vue({
				el: '#app',
				// 将路由对象绑定到vue实例中
				router
			});
		</script>
		
		
		
	</body>
</html>

3.2 举例:编程式路由使用

								// 编程式路由
								//跳转到另外一个页面
								this.$router.push('/loginsuccess')	

整体代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<!-- 引入vue -->
		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
		
		<script src="https://unpkg.com/vue-router@2.0.0/dist/vue-router.js" type="text/javascript"></script>
		<!-- 引入elementUI -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
		<script src="https://unpkg.com/element-ui/lib/index.js"></script>
	</head>
	
	
	<body>
		
		<div id="app">
			<div class="box" style="width: 600px;height: 400px; margin:100px auto;text-align: center;">
				<el-card class="box-card">
					<h1> 用户登录</h1>
					<el-form ref="formLogin" :model="formLogin" label-width="80px">
						<el-form-item label="账号">
							<el-input v-model="formLogin.user"></el-input>
						</el-form-item>
						<el-form-item label="密码">
							<el-input v-model="formLogin.pwd" type='password'></el-input>
						</el-form-item>
						<el-form-item>
							<el-button type="primary" @click="loginHandle">点击登录</el-button>
						</el-form-item>
					</el-form>
				</el-card>
			</div>
			<!-- 路由视图 -->
			<router-view></router-view>
			
		</div>
		
		
		
		<script type="text/javascript">
			
			// 定义组件样式
			// 登录页面的组件
			const homepage = {template:`<h1>-----首页--------</h1>`}
			// 首页组件
			const loginpage = {template:`<h1>===登录页===</h1>`}
			
			// 登录成功页面
			const loginsuccess = {template:`<h1>===欢迎你登录成功===</h1>`}
			
			// 定义路由规则
			const routes = [
				{path:'/home',component: homepage },
				{path:'/login',component: loginpage},
				{path:'/loginsuccess',component: loginsuccess },
			]
			
			// 创建一个路由对象,创建路由实例并传递 `routes` 配置
			const router = new VueRouter({
				routes, // `routes: routes` 的缩写
			})
			
			var vm = new Vue({
				el: '#app',
				// 将路由对象绑定到vue实例中
				data: {
					formLogin: {
						user: 'miaojiang',
						pwd: '123456'
					},
					projects: [],
				},
				methods: {
					// 点击登录之后,处理登录的方法
					loginHandle: async function() {
						// 请求登录接口
						console.log('请求登录接口')
						if(this.formLogin.user=='miaojiang'){
							if(this.formLogin.pwd=='123456'){
								console.log('登录成功')
								// 编程式路由
								//跳转到另外一个页面
								this.$router.push('/loginsuccess')								
							}							
						}
						}
				},
				router,
				
			});
		</script>
		
		
		
	</body>
</html>

输入账号密码miaojiang 123456,点击登录时

调用

 跳转路由 '/loginsuccess'

3.2.1 编程式路由,跳转时传递参数

1、路由跳转时,传递一个对象

								// 编程式路由
								//跳转到另外一个页面
								this.$router.push({
									name:'user',
									params: {
										id: this.formLogin.user
									},
									
								})	

2、跳转后到的组件,接收对象

{{ $route.params.id }}

如:

			// 登录成功页面
			const loginsuccess = {template:`<h1>===欢迎{{ $route.params.id }}登录成功===</h1>`}

整体代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<!-- 引入vue -->
		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
		
		<script src="https://unpkg.com/vue-router@2.0.0/dist/vue-router.js" type="text/javascript"></script>
		<!-- 引入elementUI -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
		<script src="https://unpkg.com/element-ui/lib/index.js"></script>
	</head>
	
	
	<body>
		
		<div id="app">
			<div class="box" style="width: 600px;height: 400px; margin:100px auto;text-align: center;">
				<el-card class="box-card">
					<h1> 用户登录</h1>
					<el-form ref="formLogin" :model="formLogin" label-width="80px">
						<el-form-item label="账号">
							<el-input v-model="formLogin.user"></el-input>
						</el-form-item>
						<el-form-item label="密码">
							<el-input v-model="formLogin.pwd" type='password'></el-input>
						</el-form-item>
						<el-form-item>
							<el-button type="primary" @click="loginHandle">点击登录</el-button>
						</el-form-item>
					</el-form>
				</el-card>
			</div>
			<!-- 路由视图 -->
			<router-view></router-view>
			
		</div>
		
		
		
		<script type="text/javascript">
			
			// 定义组件样式
			// 登录页面的组件
			const homepage = {template:`<h1>-----首页--------</h1>`}
			// 首页组件
			const loginpage = {template:`<h1>===登录页===</h1>`}
			
			// 登录成功页面
			const loginsuccess = {template:`<h1>===欢迎{{ $route.params.id }}登录成功===</h1>`}
			
			// 定义路由规则
			const routes = [
				{path:'/home',component: homepage },
				{path:'/login',component: loginpage},
				{path:'/loginsuccess/:id',component: loginsuccess,name: 'user'},
			]
			
			// 创建一个路由对象,创建路由实例并传递 `routes` 配置
			const router = new VueRouter({
				routes, // `routes: routes` 的缩写
			})
			
			var vm = new Vue({
				el: '#app',
				// 将路由对象绑定到vue实例中
				data: {
					formLogin: {
						user: 'miaojiang',
						pwd: '123456'
					},
					projects: [],
				},
				methods: {
					// 点击登录之后,处理登录的方法
					loginHandle: async function() {
						// 请求登录接口
						console.log('请求登录接口')
						if(this.formLogin.user=='miaojiang'){
							if(this.formLogin.pwd=='123456'){
								console.log('登录成功')
								// 编程式路由
								//跳转到另外一个页面
								this.$router.push({
									name:'user',
									params: {
										id: this.formLogin.user
									},
									
								})														
							}							
						}
						}
				},
				router,
				
			});
		</script>
		
		
		
	</body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/3587.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

算法:贪婪算法、分而治之

算法&#xff1a;贪婪算法、分而治之 文章目录1.贪婪算法计数硬币实例12.分而治之分割/歇征服/解决合并/合并实例23.动态规划对照实例34.基本概念算法数据定义数据对象内置数据类型派生数据类型基本操作1.贪婪算法 设计算法以实现给定问题的最佳解决方案。在贪婪算法方法中&am…

中国蚁剑AntSword实战

中国蚁剑AntSword实战1.基本使用方法2.绕过安全狗连接3.请求包修改UA特征伪造RSA流量加密4.插件使用1.基本使用方法 打开蚂蚁宝剑&#xff0c;右键添加数据&#xff1a; 输入已经上传马的路径和连接密码&#xff1a; 测试连接&#xff0c;连接成功&#xff01; GetShell了&…

【Linux】权限详解

前言首先我们先来看一下权限的概念&#xff1a;在多用户计算机系统的管理中&#xff0c;权限&#xff08;privilege&#xff09;是指某个特定的用户具有特定的系统资源使用权力&#xff0c;像是文件夹&#xff0c;特定系统指令的使用或存储量的限制。通常&#xff0c;系统管理员…

动态内存管理详细讲解

目录 1.为什么存在动态内存分配 2. 动态内存函数的介绍 2.1 malloc和free 2.2 calloc 2.3 realloc 今天要和大家分享的内容是的动态内存管理&#xff0c;我们先从他的定义入手学习。 1.为什么存在动态内存分配 我们到现在已经掌握了内存开辟的方式就是要么创建一个变量…

【差分数组】

差分数组一维差分差分数组的作用差分矩阵结语一维差分 输入一个长度为 n 的整数序列。接下来输入 m个操作&#xff0c;每个操作包含三个整数 l,r,c&#xff0c;表示将序列中 [l,r] 之间的每个数加上 c &#xff0c;请你输出进行完所有操作后的序列。 输入格式 第一行包含两个…

ArduPilot飞控之ubuntu22.04-Gazebo模拟

ArduPilot飞控之ubuntu22.04-Gazebo模拟1. 源由2. Gazebo安装2.1 ubuntu22.04系统更新2.2 安装Gazebo Garden2.3 安装ArduPilot Gazebo插件2.3.1 基础库安装2.3.2 源代码编译2.3.3 配置插件2.4 测试Gazebo Garden3. ArduPilot SITL Gazebo模拟3.1 Gazebo Garden模拟环境3.2 Ar…

大数据周会-本周学习内容总结07

目录 01【hadoop】 1.1【编写集群分发脚本xsync】 1.2【集群部署规划】 1.3【Hadoop集群启停脚本】 02【HDFS】 2.1【HDFS的API操作】 03【MapReduce】 3.1【P077- WordCount案例】 3.2【P097-自定义分区案例】 历史总结 01【hadoop】 1.1【编写集群分发脚本xsync】…

【Spring从成神到升仙系列 四】从源码分析 Spring 事务的来龙去脉

&#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是爱敲代码的小黄&#xff0c;独角兽企业的Java开发工程师&#xff0c;CSDN博客专家&#xff0c;阿里云专家博主&#x1f4d5;系列专栏&#xff1a;Java设计模式、数据结构和算法、Kafka从入门到成神、Kafka从成神到升仙…

YOLOv7训练自己的数据集(手把手教你)

YOLOv7训练自己的数据集整个过程主要包括&#xff1a;环境安装----制作数据集----模型训练----模型测试----模型推理 一&#xff1a;环境安装 conda create -n yolov7 python3.8 conda activate yolov7 #cuda cudnn torch等版本就不细说了&#xff0c;根据自己的显卡配置自行…

水果新鲜程度检测系统(UI界面+YOLOv5+训练数据集)

摘要&#xff1a;水果新鲜程度检测软件用于检测水果新鲜程度&#xff0c;利用深度学习技术识别腐败或损坏的水果&#xff0c;以辅助挑拣出新鲜水果&#xff0c;支持实时在线检测。本文详细介绍水果新鲜程度检测系统&#xff0c;在介绍算法原理的同时&#xff0c;给出Python的实…

给准备面试网络工程师岗位的应届生一些建议

你听完这个故事&#xff0c;应该会有所收获。最近有一个23届毕业的大学生和我聊天&#xff0c;他现在网络工程专业大四&#xff0c;因为今年6、7月份的时候毕业&#xff0c;所以现在面临找工作的问题。不管是现在找一份实习工作&#xff0c;还是毕业后找一份正式工作&#xff0…

100天精通Python丨基础知识篇 —— 03、Python基础知识扫盲(第一个Python程序,13个小知识点)

文章目录&#x1f41c; 1、Python 初体验Pycharm 第一个程序交互式编程第一个程序&#x1f41e; 2、Python 引号&#x1f414; 3、Python 注释&#x1f985; 4、Python 保留字符&#x1f42f; 5、Python 行和缩进&#x1f428; 6、Python 空行&#x1f439; 7、Python 输出&…

Linux基础知识点总结

♥️作者&#xff1a;小刘在C站 ♥️个人主页&#xff1a;小刘主页 ♥️每天分享云计算网络运维课堂笔记&#xff0c;努力不一定有收获&#xff0c;但一定会有收获加油&#xff01;一起努力&#xff0c;共赴美好人生&#xff01; ♥️夕阳下&#xff0c;是最美的绽放&#xff0…

史上最详细的改良顺序表讲解,看完不会你打我

目录 0.什么是顺序表 1.顺序表里结构体的定义 2.顺序表的初始化 3.顺序表的输入 4.增加顺序表的长度 5.1顺序表的元素查找&#xff08;按位查找&#xff09; 5.2顺序表的元素查找&#xff08;按值查找&#xff09;在顺序表进行按值查找&#xff0c;大概只能通过遍历的方…

HFish蜜罐的介绍和简单测试(三)

在学习蜜罐时&#xff0c;HFish是个不错的选择。首先是免费使用&#xff0c;其次易于安装管理&#xff0c;然后文档支持比较丰富&#xff0c;最后还有更多扩展功能。第三篇的话作为本系列的最终篇章进行总结&#xff0c;具体是看到哪里写到哪里。 0、HFish平台管理 0.1、报告…

基于SpringBoot实现冬奥会运动会科普平台【源码+论文】

基于SpringBoot实现冬奥会科普平台演示开发语言&#xff1a;Java 框架&#xff1a;springboot JDK版本&#xff1a;JDK1.8 服务器&#xff1a;tomcat7 数据库&#xff1a;mysql 5.7 数据库工具&#xff1a;Navicat11 开发软件&#xff1a;eclipse/myeclipse/idea Maven包&#…

一文吃透SpringBoot整合mybatis-plus(保姆式教程)

✅作者简介&#xff1a;2022年博客新星 第八。热爱国学的Java后端开发者&#xff0c;修心和技术同步精进。 &#x1f34e;个人主页&#xff1a;Java Fans的博客 &#x1f34a;个人信条&#xff1a;不迁怒&#xff0c;不贰过。小知识&#xff0c;大智慧。 &#x1f49e;当前专栏…

23.3.26总结

康托展开 是一个全排列与自然数的映射关系&#xff0c;康托展开的实质是计算当前序列在所有从小到大的全排列中的顺序&#xff0c;跟其逆序数有关。 例如&#xff1a;对于 1,2,3,4,5 来说&#xff0c;它的康托展开值为 0*4&#xff01;0*3&#xff01;0*2&#xff01;0*1&…

Android 之 打开相机 打开相册

Android 之 打开系统摄像头拍照 打开系统相册&#xff0c;并展示1&#xff0c;清单文件 AndroidManifest.xml<uses-permission android:name"android.permission.INTERNET" /><!--文件读取权限--><uses-permission android:name"android.permiss…

网络编程2(套接字编程)

套接字编程UDP协议通信&#xff1a;TCP通信&#xff1a;套接字编程&#xff1a;如何编写一个网络通信程序 1.网络通信的数据中都会包含一个完整的五元组&#xff1a; sip&#xff0c;sport&#xff0c;dip&#xff0c;dport&#xff0c;protocol&#xff08;源IP&#xff0c;源…