async changeNumber(target) { let param = { url: '/cart/number', argument: { id: localStorage.getItem('userId'), goodsId: target.parentElement.parentElement.dataset.goodsid, // number: --target.nex tElementSibling.value } } if (target.className == 'deleteGoods') { param.argument.number = --target.nextElementSibling.value if (param.argument.number == 0) { target.nextElementSibling.value = 1 target.disabled = true return } } if (target.className == 'addGoods') { param.argument.number = ++target.previousElementSibling.value target.previousElementSibling.previousElementSibling.disabled = false } let res = await ajax_post(param) if (res.code == 1) { this.renderCarList() } } 这段代码用同步函数怎么学
时间: 2023-06-09 21:07:38 浏览: 128
您好,首先需要将 async 改为 function,即将该函数改为同步函数,同时需要在异步请求部分加上 await 关键字以等待请求完成。
修改后的代码如下:
function changeNumber(target) {
let param = {
url: '/cart/number',
argument: {
id: localStorage.getItem('userId'),
goodsId: target.parentElement.parentElement.dataset.goodsid,
// number: --target.nextElementSibling.value
}
}
if (target.className == 'deleteGoods') {
param.argument.number = --target.nextElementSibling.value
if (param.argument.number == 0) {
target.nextElementSibling.value = 1
target.disabled = true
return
}
}
if (target.className == 'addGoods') {
param.argument.number = target.previousElementSibling.value
target.previousElementSibling.previousElementSibling.disabled = false
}
let res = await ajax_post(param)
if (res.code == 1) {
this.renderCarList()
}
}
阅读全文