vue2、vue3基于js给后端接口返回的数据(如以json数据为元素的数组)添加新的json字段

vue2、vue3基于js给后端接口返回的数据(如以json数据为元素的数组)添加新的json字段

1、 res为后端接口的响应

res.data.data.list = [
	{
		id:"1",
		name:"Lily"
	},
	{
		id:"2",
		name:"Lucy"
	},
]

2、 获取后端接口返回的数据

this.tableData = res.data.data.list;

3、 vue2 向数组中添加一个元素,在{}对象中添加一个字段(属性)

1. vue2 向 tableData 添加字段

1.1. 向 tableData 中添加一个新json元素( {“time”, “2023-02-09”} )
this.$set(this.tableData,'time','2023-02-09')
1.2. tableData 添加新元素后 tableData 的值
this.tableData = [
	{
		id:"1",
		name:"Lily"
	},
	{
		id:"2",
		name:"Lucy"
	},
	{
		time: "2023-02-09",
	},
]

2. vue2 向 tableData 的元素添加字段

2.1. 向 tableData 中的每个元素添加一个新json元素( {“age”, “20”+i} )
for(let i=0; i<this.tableData.length; i++) {
	this.$set(this.tableData[i],'age','20'+i)
}
2.2. 向 tableData 中的每个元素添加一个新json元素后tableData 的数据
this.tableData = [
	{
		id:"1",
		name:"Lily",
		age:"21"
	},
	{
		id:"2",
		name:"Lucy",
		age:"22"
	},
	{
		time: "2023-02-09",
		age:"23"
	},
]

4、 vue3 向数组中添加一个元素,在{}对象中添加一个字段(属性)

1. vue3 向 tableData 添加字段

1.1. 向 tableData 中添加一个新json元素( {“time”, “2023-02-09”} )
// 添加
// 在数组的索引位置1的前面(即索引位置0的后面)添加一个元素
this.tableData.value.splice(1, 0, {"time", "2023-02-09"});

// 替换:
// 将数组的索引2位置的元素替换为 {"phone", "12345678901"}
this.tableData.value.splice(2, 1, {"phone", "12345678901"});
1.2. tableData 添加新元素和替换元素后 tableData 的值
this.tableData = [
	{
		id:"1",
		name:"Lily"
	},
	{
		time: "2023-02-09",
	},
	{
		phone: "12345678901",
	},
]

2. vue3 向 tableData 的元素添加字段

2.1. 向 tableData 中的每个元素添加一个新json元素( {“age”, “20”+i} )
for(let i=0; i<this.tableData.length; i++) {
	this.tableData[i].age = "20"+i;
}
2.2. 向 tableData 中的每个元素添加一个新json元素后tableData 的数据
this.tableData = [
	{
		id:"1",
		name:"Lily",
		age:"21"
	},
	{
		time: "2023-02-09",,
		age:"21"
	},
	{
		phone: "12345678901",,
		age:"21"
	},
]

学习:Web 开发技术

### Vue3 和 SpringBoot 后端返回 JSON 数据到前端的示例 在 Vue3 和 SpringBoot 框架下,后端通过 RESTful API 返回 JSON 数据给前端是一种常见的交互方式。以下是一个完整的示例,展示了如何实现这一功能。 #### 1. SpringBoot 后端代码 SpringBoot 使用 `@RestController` 注解来定义一个控制器类,并使用 `@PostMapping` 或 `@GetMapping` 来处理 HTTP 请求。后端可以通过 `ResponseEntity` 或直接返回对象,框架会自动将其序列化为 JSON 格式[^1]。 ```java @RestController @RequestMapping("/api") public class UserController { @PostMapping("/user") public ResponseEntity<User> createUser(@RequestBody User user) { // 处理逻辑 return ResponseEntity.ok(user); // 返回 JSON 数据 } @GetMapping("/user/{id}") public ResponseEntity<User> getUserById(@PathVariable Long id) { // 查询逻辑 User user = new User(); user.setId(id); user.setName("Test User"); return ResponseEntity.ok(user); // 返回 JSON 数据 } } ``` #### 2. Vue3 前端代码 Vue3 中可以使用 `axios` 库来发送 HTTP 请求并接收 JSON 数据。以下是一个完整的示例,展示如何从后端获取数据并渲染到页面上[^3]。 ```javascript <template> <div> <h1>User Information</h1> <p>ID: {{ user.id }}</p> <p>Name: {{ user.name }}</p> <button @click="fetchUser">Fetch User</button> </div> </template> <script> import axios from &#39;axios&#39;; export default { data() { return { user: {} }; }, methods: { async fetchUser() { try { const response = await axios.get(&#39;http://localhost:8080/api/user/1&#39;); // 调用后端接口 this.user = response.data; // 将返回JSON 数据赋值给 user 对象 } catch (error) { console.error(&#39;Error fetching user:&#39;, error); } } } }; </script> ``` #### 3. JSON 数据格式 后端返回JSON 数据通常是一个对象或数组。例如,上述代码中返回JSON 数据可能如下所示: ```json { "id": 1, "name": "Test User" } ``` #### 4. 注意事项 - 确保后端返回对象已经被正确序列化为 JSON 格式。如果需要自定义序列化规则,可以使用 Jackson 的注解,例如 `@JsonInclude` 或 `@JsonIgnore`[^1]。 - 如果前端传递了多余的参数,SpringBoot 默认会抛出异常。为了兼容这种情况,可以在后端使用 `@JsonFilter` 或者创建一个通用的请求参数类来处理多余字段[^2]。 - 在 Vue3 中,确保已经安装并配置了 `axios` 库。如果没有,请运行以下命令进行安装: ```bash npm install axios ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值