更改为使用自定义下拉列表后看不到父变量?



我有一个包含 2 个直接子级的组件,这两个组件都使用父级中保存的event变量。但是,在使用下拉列表的组件之一从使用<select>更改为自定义动画下拉列表时......我再也看不到这个孩子内心的event,尽管使用了几乎相同的代码。

event: IEvent;
constructor(private eventService: EventService) {
}
ngOnInit() {
this.subToEventService();
}
subToEventService() {
this.eventService.eventSubject
.subscribe(res => {
this.event = res;
}
}

儿童 1(可查看活动(

export class ChildOne extends ParentComponent implements OnInit {
constructor(eventService: EventService) {
super(eventService);
}
ngOnInit() {
console.log(this.event);
}
}

儿童 2(无法查看事件(

export class ChildTwo extends ParentComponent implements OnInit {
@ViewChild('dropdown') dropdown: ElementRef;
expanded = false;
constructor(eventService: EventService) {
super(eventService);
}
ngOnInit() {
console.log(this.event);
}
toggleDropdown() {
const dropdown = this.dropdown.nativeElement;
if (this.expanded) {
TweenMax.to(dropdown, 0.5, {...});
} else {
TweenMax.to(dropdown, 0.5, {...});
}
this.expanded = !this.expanded;
}
determineStyle() {
const style = this.dropdown.nativeElement.style;
style.height = this.expanded ? 376 : 34;
}
}

在这两个子组件中,您都将重写ParentComponent类的ngOnInit方法。

您需要在两个子ngOnInit上调用super.ngOnInit();,以订阅这些组件中的主题。

最新更新