对于Ember版本2.16
,他们已删除传递给didReceiveAttrs
组件生命周期挂钩的参数。以前,我以对象的形式获取参数。newAttrs:
EmptyObject:
{
dataTestId: "test-object"
items: MutableCell {__MUTABLE_CELL__ [id=__ember1555539649631463096066386]: true, __REF__ [id=__ember1555539649631670217952659]: RootPropertyReference, value: Array(27)}
prompt: SafeString {string: "Select an Initiator Group"}
required: true
selected: MutableCell {__MUTABLE_CELL__ [id=__ember1555539649631463096066386]: true, __REF__ [id=__ember1555539649631670217952659]: RootPropertyReference, value: undefined}
__proto__: Object
}
弃用后如何在
didReceiveAttrs
中获得这样的对象? 最佳答案
嗯,这是2.12
中添加的弃用。直到2.13.0
,您都可以在组件生命周期挂钩中获取参数。
下面是在2.13.0
之后获取挂钩参数的另一种方法,
之前:
didReceiveAttrs({ oldAttrs, newAttrs }) {
if (oldAttrs.temp !== newAttrs.temp) {
this.thermometer.move({ from: oldAttrs.temp, to: newAttrs.temp });
}
}
后:
didReceiveAttrs() {
let oldTemp = this.get('_oldTemp');
let newTemp = this.get('temp');
if (oldTemp && oldTemp !== newTemp) {
this.thermometer.move({ from: oldTemp, to: newTemp });
}
this.set('_oldTemp', newTemp);
}
您可以从官方deprecation guide获取更多信息