this在各類回調中使用:
如果是普通函數是沒法使用的
所以需要先將this.變量賦值給新的變量,然后才能在回調函數中使用
例如:
refund: function (id) {
if (!this.url.refund) {
this.$message.error("請設置url.refund屬性!")
return
}
var that = this;
this.$confirm({
title: "確認退款",
content: "是否要進行退款?",
onOk: function () {
putAction(that.url.refund, { "id": id }).then((res) => {
if (res.success) {
that.$message.success(res.message);
that.loadData();
} else {
that.$message.warning(res.message);
}
});
}
});
},
如果是箭頭函數式可以使用的
下面的這個例子只是參考,並不代表this.$confirm就是這么使用,具體參考ant的文檔
下面的例子只是代表,箭頭函數可以使用this
efund: function (id) {
if (!this.url.refund) {
this.$message.error("請設置url.refund屬性!")
return
}
this.$confirm({
title: "確認退款",
content: "是否要進行退款?",
onOk: () => {
putAction(that.url.refund, { "id": id }).then((res) => {
if (res.success) {
this.$message.success(res.message);
this.loadData();
} else {
this.$message.warning(res.message);
}
});
}
});
},