Component 間的溝通
假設我們想在 nav bar 上加一個 重新整理 的按鈕, 在按下按鈕時重新載入資料,要該怎麼做,才能正確的觸發資料更新,並進一步顯示在頁面上?
首先,要調整一下 NavBarComponent 如下:
import { Component, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.css']
})
export class NavbarComponent {
constructor() { }
@Output() onRefresh: EventEmitter<null> = new EventEmitter<null>();
refresh() {
this.onRefresh.emit();
}
}
<button class="btn btn-success" type="button" (click)="refresh()">Reload</button>
<app-navbar (onRefresh)="dashboard.generateData()"></app-navbar>
<app-dashboard #dashboard></app-dashboard>
小結
When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.
評論