在子组件初始化后,将值从子组件传递到父组件

Pass value from Child component to Parent component , after child is initialized

现在我在单击发送按钮时将值从子项传递到父项,如何传递相同的数组子项已初始化,我尝试使用 '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>

ngOnInit() 应该足以满足您的情况,因为您传递的数据不依赖于 "view"。 在这里查看:StackBlitz example