思路:
-
这个vant 计步器的加减 ,有一个事件:当绑定值变化时触发的事件,change
1.1 使用这个事件,绑定到对应要使用的地方, 注意: change事件默认有两个回调参数,第一个是改变数量的value值 要传递当前的id给后端要这样写,不然无法传参,反正我也很不理解现在:changeNum($event,item.id) <van-stepper @change="changeNum($event,item.id)" v-model="item.num" integer /> // 更改商品数量,同步到后端数据库中 changeNum(value, id) { // value 是数量,id是传递过来的对应商品表的id值 console.log(value, id); }, -
前端
// 更改商品数量,同步到后端数据库中
async changeNum(value, id) {
// value 是数量,id是传递过来的对应商品的id值
await http.$axios({
method: "POST",
url: "api/updateNum",
// 前端发请求带上 headers,参数为token
headers: {
token: true,
},
data: {
value,
id,
},
});
},
3. 后端接口:先根据前端传过来的id, 这里注意一点,多个function里的内容不要写相同的名字,否则容易出错
// 更新数据库中商品的num值
router.post('/api/updateNum', function (req, res, next) {
// 前端传递过来的商品数量 和 对应的商品id
let {value,id} = req.body
connection.query(`select * from cartlist where id = "${id}"`, function (e, r) {
if (r) {
// let num = r[0].num; // 原本的商品数量
connection.query(`update cartlist set num = "${value}" where id = "${id}"`, function (e, result) {
// console.log(r,"adfasdfa");
if (result) {
res.send({
data: {
code: 200,
success: true,
}
})
}
})
}
})
})