概述
Vue.js要获取对象的值,不能像普通值那样设置,对象的每个字段都需要在data(){return{}}
中进行设置,然后我们才可以获取到对象的值,对象的对象的值也可以获取到
实现过程
实体类
秒杀商品实体类内有一个组合:商品实体类,也就是说,查询秒杀商品时也同时查询到对应的商品
商品实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| package com.ledao.entity;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data;
import java.io.Serializable;
@Data @TableName("t_goods") public class Goods implements Serializable {
@TableId private Integer id;
private String name;
private Double price;
private String image;
private Integer stock;
private String detail;
private static final long serialVersionUID = 1L; }
|
秒杀商品实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| package com.ledao.entity;
import java.io.Serializable; import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data;
@Data @TableName("t_miaosha_goods") public class MiaoShaGoods implements Serializable {
@TableId private Integer id;
@TableField(value = "goodsId") private Integer goodsId;
@TableField(exist = false) private Goods goods;
private Double price;
private Integer stock;
@TableField(value = "startTime") private Date startTime;
@TableField(value = "endTime") private Date endTime;
private static final long serialVersionUID = 1L; }
|
Vue代码
数据绑定模板如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| data() { return { miaoShaGoods: { id: 0, goodsId: 0, price: 0, stock: 0, startTime: '', endTime: '', goods: { id: 0, name: '', price: 0, image: '', stock: 0, detail: '' } } } }
|
下面的代码请求了后端,并把请求到的数据绑定到Vue,然后可以通过{{miaoShaGoods.goods.id}}
查到秒杀商品对应的商品id了
1 2 3 4 5 6 7 8 9 10 11 12
| openMiaoShaDialog: function (id) { let _this = this; let url = getServerUrl("miaoShaGoods/findById?id=" + id); axios .get(url) .then(function (response) { _this.miaoShaGoods = response.data; }) .catch(function (error) { console.log(error); }); }
|