簡體   English   中英

子組件初始化后,將值從子組件傳遞給父組件

[英]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() 應該足以滿足您的情況,因為您傳遞的數據不依賴於“視圖”。 在這里查看: StackBlitz 示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM