Ember 组件计算函数在数据更改时不会重新运行
Ember component computed function does not rerun when data changes
我在计算的 属性 中存储了一个字符串值,如下所示:
clientId: Ember.computed.oneWay("_clientId")
其中 _clientId 在对象上定义为 属性,如下所示:
export default Ember.service.extend { _clientId: null, clientId: Ember.computed.oneWay("_clientId"), updateId() {this.set("_clientId", "fe48822d-bf50-44a1-9ce0-61b06504d726"); } }
然后我有一个带有计算 属性 的组件,如下所示:
chartData: Ember.computed("data", function () {
const data = this.get("data");
const clientId = this.get("session").get("clientId");
console.log(clientId);
if (data && data.get("left") !== undefined && data.get("right") !== undefined) {
return data;
}
this.set("_chartDisplayData", null);
return null;
})
当我调用 updateId 时,我希望 chartData 函数重新 运行 因为 clientId 的值已更改(我验证了 clientId 的值已更改)。但是,chartData 函数从不重运行s,这是为什么呢?
您需要告诉 computed property about all of your dependencies. First, the computed property will never run if it isn't being used somewhere. If you aren't using it you need an observer。但假设您实际使用它,计算的 属性 只会在列出的依赖项之一发生变化时重新计算自己。如果您将一个对象列为依赖项,那么如果仅更改该对象的某些属性,它不会更新,只有在整个对象被替换时才会更新。试试这个:
chartData: Ember.computed("data.left", "data.right", "session.clientId", function () {
const data = this.get("data");
const clientId = this.get("session.clientId");
console.log(clientId);
if (data && data.get("left") !== undefined && data.get("right") !== undefined) {
return data;
}
this.set("_chartDisplayData", null);
return null;
})
我在计算的 属性 中存储了一个字符串值,如下所示:
clientId: Ember.computed.oneWay("_clientId")
其中 _clientId 在对象上定义为 属性,如下所示:
export default Ember.service.extend { _clientId: null, clientId: Ember.computed.oneWay("_clientId"), updateId() {this.set("_clientId", "fe48822d-bf50-44a1-9ce0-61b06504d726"); } }
然后我有一个带有计算 属性 的组件,如下所示:
chartData: Ember.computed("data", function () {
const data = this.get("data");
const clientId = this.get("session").get("clientId");
console.log(clientId);
if (data && data.get("left") !== undefined && data.get("right") !== undefined) {
return data;
}
this.set("_chartDisplayData", null);
return null;
})
当我调用 updateId 时,我希望 chartData 函数重新 运行 因为 clientId 的值已更改(我验证了 clientId 的值已更改)。但是,chartData 函数从不重运行s,这是为什么呢?
您需要告诉 computed property about all of your dependencies. First, the computed property will never run if it isn't being used somewhere. If you aren't using it you need an observer。但假设您实际使用它,计算的 属性 只会在列出的依赖项之一发生变化时重新计算自己。如果您将一个对象列为依赖项,那么如果仅更改该对象的某些属性,它不会更新,只有在整个对象被替换时才会更新。试试这个:
chartData: Ember.computed("data.left", "data.right", "session.clientId", function () {
const data = this.get("data");
const clientId = this.get("session.clientId");
console.log(clientId);
if (data && data.get("left") !== undefined && data.get("right") !== undefined) {
return data;
}
this.set("_chartDisplayData", null);
return null;
})