Vue前端代码过程 —— 与Spring Boot后端的数据请求演示


一、创建数据库

二、创建并启动SpringBoot项目

三、创建Vue项目

前端代码编写软件:HbuilderX
代码测试浏览器:谷歌浏览器(Chrome)

1. 创建空项目

在这里插入图片描述
导入vue框架文件 vue.js
导入vue联网文件 axios.min.js

2. 登录页面演示 userlogin.html

<!DOCTYPE html> 
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="app">
			用户名:<input id="username">
			<br>
			<button v-on:click="login">登录</button>
		</div>
	</body>
	<script src="vue.js"></script>
	<script>
		// 1.创建配置对象
		var config = {
			"el": "#app",
			// mounted:定义自动调用的方法
			"mounted": function() {
				console.log("mounted运行");
			},
			"methods": {
				"login": function() {
					console.log("login()运行");
				}
			}
		}
		// 2.启动vue框架
		var vue = new Vue(config);
	</script>
</html>

运行html文件
点击登录按钮,
右键->检查->console 查看控制台输出信息
在这里插入图片描述

3. 自动生成跳转网站的参数

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="app">
			账号:<input id="username">
			<br>
			密码:<input id="password">
			<br>
			<button v-on:click="login">登录</button>
		</div>
	</body>
	<script src="vue.js"></script>
	<script src="axios.min.js"></script>
	<script>
		// 1.创建配置对象
		var config = {
			// 绑定app元素
			"el": "#app",
			// mounted:定义自动调用的方法
			"mounted": function() {
				console.log("mounted运行");
			},
			"methods": {
				"login": function() {
					console.log("login()运行");
					// 1.接收用户输入的数据
					var username = document.getElementById("username").value;
					var password = document.getElementById("password").value;

					// 2.拼出联网的地址
					var serverUrl = "http:localhost:1314/user/login?" +
						"username=" + username +
						"&password=" + password;
					console.log(serverUrl);
				}
			}
		}
		// 2.启动vue框架
		var vue = new Vue(config);
	</script>
</html>

在这里插入图片描述复制控制台输出的地址在浏览器进行访问
在这里插入图片描述

4. 联网测试

在这里插入图片描述
若操作弹出联网失败,是跨域访问的问题

在相应的Controller控制器类中,添加跨域访问允许

@CrossOrigin // 允许别的网站用axios访问

在这里插入图片描述

5. 自动跳转网址

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="app">
			账号:<input id="username">
			<br>
			密码:<input id="password">
			<br>
			<button v-on:click="login">登录</button>
		</div>
	</body>
	<script src="vue.js"></script>
	<script src="axios.min.js"></script>
	<script>
		// 1.创建配置对象
		var config = {
			// 绑定app元素
			"el": "#app",
			// mounted:定义自动调用的方法
			"mounted": function() {
				console.log("mounted运行");
			},
			"methods": {
				"login": function() {
					console.log("login()运行");
					// 1.接收用户输入的数据
					var username = document.getElementById("username").value;
					var password = document.getElementById("password").value;

					// 2.拼出联网的地址
					var serverUrl = "http:localhost:1314/user/login?" +
						"username=" + username +
						"&password=" + password;
					console.log(serverUrl);

					// 3.用axios联网
					// 联网成功调用then()
					// 联网失败调用catch()
					axios.get(serverUrl)
					.then(function(res){
						window.alert("then:联网成功");
						var controllerResult = res.data;
						console.log("controllerResult");
						if(controllerResult){
							window.alert("登录成功");
						}
					})
					.catch(function(){
						window.alert("catch:联网失败")
					});
				}
			}
		}
		// 2.启动vue框架
		var vue = new Vue(config);
	</script>
</html>

6. 端口连接测试

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="app">
			账号:<input id="username" />
			<br>
			密码:<input id="password" />
			<br>
			<button v-on:click="login">登录</button>
		</div>
	</body>
	<script src="vue.js"></script>
	<script src="axios.min.js"></script>
	<script>
		// 1.创建配置对象
		var config = {
			"el": "#app",
			"mounted": function() {
				console.log("mounted运行");
			},
			"methods": {
				"login": function() {
					console.log("login()运行")
					// 1.接收用户输入的数据
					var username = document.getElementById("username").value;
					var password = document.getElementById("password").value;

					// 2.拼出联网的地址:地址为UserController中login的网址
					var serverUrl = "http://localhost:1314/user/login?" +
						"username=" + username +
						"&password=" + password;
					console.log(serverUrl);
					// 右键 检查,找开调试窗口 console,看到serverUrl,双击访问url,不能显示 404,500

					// 3.用axios联网
					// 联网失败调用catch()
					// 联网成功调用then()
					axios.get(serverUrl)
						.then(function(res) {
							window.alert("then:联网成功")
							var controllerResult = res.data;
							console.log(controllerResult);
							if (controllerResult.id >= 1) {
								window.alert("登录成功")
							} else {
								window.alert("登录失败")
							}
						})
						.catch(function() {
							window.alert("catch:联网失败")
						});
				}
			}
		}
		// 2.启动vue框架
		var vue = new Vue(config);
	</script>
</html>

在这里插入图片描述

7. 首页 index.html

端口开启测试
在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="app">
			<table width="100%">
				<tr>
					<td v-for="category in categoryList">
						{{category.name}}
					</td>
				</tr>
			</table>
		</div>
	</body>
	<script src="vue.js"></script>
	<script src="axios.min.js"></script>
	<script>
		var config={
			"el":"#app",
			"data":{
				categoryList:[]
			},
			"mounted":function(){
				// selectAll()联网
				var serverUrl = "http://localhost:1314/category/selectAll";
				axios.get(serverUrl)
				.then(function(res){
					window.alert("then:联网成功")
					//给data中的集合赋值,赋值后,界面会自动更新
					this.vue.categoryList = res.data;
				})
				.catch(function(){
					window.alert("catch:联网失败")
				});
			}
		}
		var vue=new Vue(config);
	</script>
</html>

若联网失败,一般是跨越权限问题
加入@CrossOrigin 注解,允许跨域访问
在这里插入图片描述
停止后,重新运行 Spring Boot 程序,刷新网页
在这里插入图片描述在这里插入图片描述

8. 端口开启测试

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="app">
			<table width="100%">
				<tr>
					<td v-for="category in categoryList">
						{{category.name}}
					</td>
				</tr>
			</table>
		</div>
	</body>
	<script src="vue.js"></script>
	<script src="axios.min.js"></script>
	<script>
		var config = {
			"el": "#app",
			"data": {
				categoryList: []
			},
			"methods": {
				"selectItem": function(id) {
					var serverUrl = "http://localhost:1314/item/selectByCategoryId?categoryId=" + id;
					axios.get(serverUrl)
						.then(function(res) {
							window.alert("then:联网成功")
						})
						.catch(function() {
							window.alert("catch:联网失败")
						});
				}
			},
			"mounted": function() {
				// 调用methods中的selectItem()
				this.selectItem(1);
				// selectAll()联网
				var serverUrl = "http://localhost:1314/category/selectAll";
				axios.get(serverUrl)
					.then(function(res) {
						window.alert("then:联网成功")
						//给data中的集合赋值,赋值后,界面会自动更新
						this.vue.categoryList = res.data;
					})
					.catch(function() {
						window.alert("catch:联网失败")
					});
			}
		}
		var vue = new Vue(config);
	</script>
</html>

运行index.html 联网成功

9. 根据数据库图片地址,显示图片

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="app">
			<table width="100%">
				<tr>
					<td v-for="category in categoryList">
						{{category.name}}
					</td>
				</tr>
			</table>
			<table>
				<tr v-for="item in itemList">
					<td>
						{{item.name}}
						<br>
						{{item.price}}
						<br>
						<img v-bind:src="item.image" width="100" height="100" />
					</td>
				</tr>
			</table>
		</div>
	</body>
	<script src="vue.js"></script>
	<script src="axios.min.js"></script>
	<script>
		var config = {
			"el": "#app",
			"data": {
				categoryList: [],
				itemList:[]
			},
			"methods": {
				"selectItem": function(id) {
					var serverUrl = "http://localhost:1314/item/selectByCategoryId?categoryId=" + id;
					axios.get(serverUrl)
						.then(function(res) {
							window.alert("then:联网成功")
							this.vue.itemList = res.data;
						})
						.catch(function() {
							window.alert("catch:联网失败")
						});
				}
			},
			"mounted": function() {
				// 调用methods中的selectItem()
				this.selectItem(1);
				// selectAll()联网
				var serverUrl = "http://localhost:1314/category/selectAll";
				axios.get(serverUrl)
					.then(function(res) {
						window.alert("then:联网成功")
						//给data中的集合赋值,赋值后,界面会自动更新
						this.vue.categoryList = res.data;
					})
					.catch(function() {
						window.alert("catch:联网失败")
					});
			}
		}
		var vue = new Vue(config);
	</script>
</html>

查看数据库item表
在这里插入图片描述
下载相应图片,保存到项目目录中,名称与数据库相同
在这里插入图片描述

运行网页
在这里插入图片描述

10. 商品详情页面的跳转

根据点击的物品,自动匹配id参数,生成跳转网站的参数传递

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="app">
			<table width="100%">
				<tr>
					<td v-for="category in categoryList" v-on:click="selectItem(category.id)">
						{{category.name}}
					</td>
				</tr>
			</table>
			<table>
				<tr v-for="item in itemList">
					<td>
						<a v-bind:href="'detail.html?id='+item.id">
							{{item.name}}
							<br>
							{{item.price}}
							<br>
							<img v-bind:src="item.image" width="100" height="100" />
						</a>
					</td>
				</tr>
			</table>
		</div>
	</body>
	<script src="vue.js"></script>
	<script src="axios.min.js"></script>
	<script>
		var config = {
			"el": "#app",
			"data": {
				categoryList: [],
				itemList: []
			},
			"methods": {
				"selectItem": function(id) {
					var serverUrl = "http://localhost:1314/item/selectByCategoryId?categoryId=" + id;
					axios.get(serverUrl)
						.then(function(res) {
							//window.alert("then:联网成功")
							this.vue.itemList = res.data;
						})
						.catch(function() {
							window.alert("catch:联网失败")
						});
				}
			},
			"mounted": function() {
				// 调用methods中的selectItem()
				this.selectItem(1);
				// selectAll()联网
				var serverUrl = "http://localhost:1314/category/selectAll";
				axios.get(serverUrl)
					.then(function(res) {
						//window.alert("then:联网成功")
						//给data中的集合赋值,赋值后,界面会自动更新
						this.vue.categoryList = res.data;
					})
					.catch(function() {
						window.alert("catch:联网失败")
					});
			}
		}
		var vue = new Vue(config);
	</script>
</html>

点击第一台手机链接,导向该地址,自动匹配id的值 = 1

在这里插入图片描述

11. 商品详情页面

端口测试
在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="app">
			{{item.name}}<br>
			{{item.price}}<br>
			<img v-bind:src="item.image" />
			<br>
			{{item.itemDesc}}
		</div>
	</body>
	<script src="vue.js"></script>
	<script src="axios.min.js"></script>
	<script>
		var config = {
			"el": "#app",
			"data": {
				item: null
			},
			"mounted": function() {
				console.log("mounted运行了")
				// 接收商品的编号,在index.html中单击商品 detail.html?id=2
				var p = location.search;
				console.log(p); // ?id=2
				p = p.substr(1);
				console.log(p); // id=2
				// 字符串分隔
				var stringArray = p.split("=");
				console.log(stringArray[0]); // id
				console.log(stringArray[1]); // 商品编号

				var serverUrl = "http://localhost:1314/item/selectByItemId?id=" + stringArray[1];
				axios.get(serverUrl)
					.then(function(res) {
						//window.alert("then:联网成功")
						this.vue.item = res.data;
					})
					.catch(function() {
						window.alert("catch:联网失败")
					})
			}
		}
		var vue = new Vue(config);
	</script>
</html>

跳转成功
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Whitemeen太白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值