如何才能点击删除按钮删除对应的商品
需要以下步骤
一、在删除按钮上绑定点击事件,并通过 scope.row.商品对应的id 这种方式获取对应的id值
<template slot-scope="scope">
<el-button size="mini" type="primary" icon="el-icon-edit"></el-button><el-button size="mini" type="danger" icon="el-icon-delete" @click="removeById(scope.row.goods_id)"></el-button>
</template>
二、在methods中定义这个函数,在element上面提供的MessageBox 弹框中将
async removeById(id) {
const confirmResult = await this.$confirm(
'此操作将永久删除该商品, 是否继续?',
'提示',
{
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}
).catch(err => err)
if (confirmResult !== 'confirm') {
return this.$message.info('已经取消删除')
}
const { data: res } = await this.$http.delete(`goods/${id}`)
if (res.meta.status !== 200) {
return this.$message.error('删除失败')
}
this.$message.success('成功删除商品')
// 刷新商品列表
this.getGoodsList()
}
}
这段代码复制粘贴到自己的代码中,把所对应的值更换一下,发送ajax请求的参数需要注意,这里是用id值来确定所要删除的商品