我正在尝试访问子属性menuType。到目前为止,我有一些代码,但是以某种方式,父级并没有更新该属性及其子级组件。
这是我的方法:
父母:
import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
import { MenuHeaderComponent } from './menu-header/menu-header.component';
@Component({
selector: 'app-menu',
templateUrl: './menu.component.html',
styleUrls: ['./menu.component.scss']
})
export class MenuComponent implements OnInit, AfterViewInit {
@ViewChild(MenuHeaderComponent) menuTypeDetail: MenuHeaderComponent;
menuType: number;
constructor() { }
ngAfterViewInit(): void {
this.getChildProperty();
}
getChildProperty(): void {
this.menuType = this.menuTypeDetail.menuType;
}
ngOnInit() {
}
}
孩子:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-menu-header',
templateUrl: './menu-header.component.html',
styleUrls: ['./menu-header.component.scss']
})
export class MenuHeaderComponent implements OnInit {
menuType = 3;
menuClicked(type): void {
this.menuType = type;
}
constructor() { }
ngOnInit() {
}
}