【发布时间】:2019-08-25 06:22:48
【问题描述】:
现在我在单击发送按钮时将值从子级传递给父级,如何传递相同的数组子级已初始化,我尝试使用“ngAfterViewInit”但没有成功,这里有任何建议。
child
@Component({
selector: 'app-child',
templateUrl: './child.component.html'
})
export class ChildComponent {
cars = ["Saab", "Volvo", "BMW", "BMW", 'Saab'];
@Output() msEvent = new EventEmitter();
constructor() {}
send() {
this.msEvent.emit(this.cars);
}
// ngAfterViewInit() {
// this.msEvent.emit(this.cars);
// }
}
}
child.component.html : <button (click) = 'send()' class="button">send</button>
----------------------------------------
parent
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
cars:any[];
constructor() {
}
rece($event) {
this.cars = $event;
}
}
app.component.html : <app-child (msEvent) = 'rece($event)' [employees] = 'employees'></app-child>
【问题讨论】:
-
在 ngAfterViewInit 中对我来说效果很好。当取消注释该代码时,您还有“导出类 ChildComponent 实现 AfterViewInit”?
标签: angular typescript angular-components