【发布时间】:2020-08-28 03:48:54
【问题描述】:
在我的 Angular 9 项目中,我有 2 个组件,它们是兄弟姐妹和 parent component。在component A 发生变化时,我发出一个值,它在parent component 中设置,并在component B 中调用一个方法。 component B 中的方法发出另一个值,并在 parent component 中设置。 component A 中的 on 更改继续,但在 parent component 中设置的 component B 发出的值(这是 component A 中的输入)没有更改。我不知道为什么即使父级更新了值,component A 的输入也不会改变。
父组件
setSomeNum(someNum: number) {
// this is run on someNumberEmitter in Component A
this.num = someNum;
if (this.viewChildComponentB) {
this.viewChildComponentB.remove(someNum);
}
}
setSomeOtherNum (someOtherNum: number) {
// this is run on someDiffNumEmitter in Component B
this.otherNum = someOtherNum
}
组件 A
componentAOnChange(someNum: number) {
this.someNumberEmitter.emit(someNum);
// this.inputFromComponentB is the original value instead of the one emitted in Component B (this.someDiffNum)
this.someService.applyCalc(someNum, this.inputFromComponentB);
}
组件 B
remove(someNum: number) {
this.someDiffNumEmitter.emit(this.someDiffNum);
this.someService.applyCalc(someNum, this.someDiffNum);
}
我正在使用OnPush 更改检测策略,但没有任何更改。同级组件 A 如何随着组件 B 的数据变化而运行?
【问题讨论】:
标签: javascript angular