繁体   English   中英

如何根据基础组件的变化检测子组件的变化 - Angular

[英]How to detect changes in child component with respect to changes in the base component - Angular

我有一个组件profile.component.ts ,其中有一个变量this.candidate ,它订阅并像这样更新

 this.candidateSub = this.store.select(selectCandidateState).subscribe((candidateState) => {
    this.candidate = candidateState;
 })

这个组件就像一个基础组件,还有一个扩展这个组件的子组件,

export class SkillsComponent extends profile implements OnInit, OnDestroy

现在我想在这个子组件中有另一个变量,只要this.candidate在基本组件中更新,它就会自行更新。 是这样的:

this.candidateSkills = this.candidate.skills

问题是我不能简单地通过在ngOnit中声明this.candidateSkills = this.candidate.skills来完成它,因为当this.candidate在基本组件中更新时,它不会观察和收听更改并更新自身。

我应该怎么做?

在父组件中:

this.storeObservable = this.store.select(selectCandidateState)

this.candidateSub = this.storeObservable.subscribe((candidateState) => {
  this.candidate = candidateState;
})

在子组件中:

this.storeObservable.subscribe((candidateState) => {
  // do something
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM