1 回答

TA贡献1815条经验 获得超13个赞
不可能将这样的设置器“复制”到局部变量中。
您可以使用自己的 setter 抽象方法或另一个对象背后的属性:
click() {
const self = this
const wrapper = {
get count() { return self.count },
set count(x) { self.count = x }
}
wrapper.count++
}
我觉得这是一个 XY 问题;你为什么要这样做?你想达到什么目的?
您最好将局部变量重新分配给 data 属性以触发更新:
click() {
let count = this.count
// Modify local var
count++
// Assign back to trigger the update
this.count = count
}
更新
您可以通过其字符串名称动态访问该属性:
hitAPI(type: "A" | "B") {
const prop = type === "A" ? "isALoading" : "isBLoading"
this[prop] = true
fetch(someAPI).finally(() => {
this[prop] = false
})
}
或者使用async/ await:
async hitAPI(type: "A" | "B") {
const prop = type === "A" ? "isALoading" : "isBLoading"
this[prop] = true
try {
await fetch(someAPI)
} finally {
this[prop] = false
}
}
添加回答
举报