本篇文章帶大家了解一下Angular中的組件通信,介紹一下父子組件間通信、無直接關(guān)系組件間通信的方法。
在實際的應(yīng)用中我們的組件將會以樹形的結(jié)構(gòu)進行關(guān)聯(lián),所以組件間的關(guān)系主要就是:
父子關(guān)系
兄弟關(guān)系
無直接關(guān)系
【相關(guān)教程推薦:《angular教程》】
準(zhǔn)備一下我們的環(huán)境:
1、創(chuàng)建一個header
組件: ng g c components/header
<app-button></app-button> <app-title></app-title> <app-button></app-button>
export class HeaderComponent implements OnInit { constructor() {} ngOnInit(): void {} }
2、創(chuàng)建一個title
組件: ng g c components/title
<span>{{title}}</span>
export class TitleComponent implements OnInit { public title: string = '標(biāo)題'; constructor() {} ngOnInit(): void {} }
3、創(chuàng)建一個button
組件: ng g c components/button
<button>{{ btnName }}</button>
export class ButtonComponent implements OnInit { public btnName: string = '按鈕'; constructor() {} ngOnInit(): void {} }
直接調(diào)用
適用于父子關(guān)系組件,注意點是直接調(diào)用使得父子組件的耦合性變高,要明確使用確實需要直接調(diào)用。
1、將我們的header組件掛載到app中,使得app和header之間形成父子組件關(guān)系
2、使用#
為我們的組件起一個名稱: <app-header #header></app-header>
3、現(xiàn)在我們的header組件還很空,我們擴展一下,要不然調(diào)用什么呢?
export class HeaderComponent implements OnInit { public name: string = 'HeaderComponent'; printName(): void { console.log('component name is', this.name); } }
4、組件擴展好以后我們就可以在父組件app中調(diào)用子組件header中的屬性和函數(shù)了
<app-header #header></app-header> <p> 調(diào)用子組件屬性: {{ header.name }} <button (click)="header.printName()">調(diào)用子組件函數(shù)</button> </p>
5、第4步是在父組件的html模板中進行操作,有時候我們還需要在父組件的ts類中對子組件進行操作,我們接下來接著演示。
6、我們需要用到一個新的裝飾器@ViewChild(Component)
export class AppComponent { title = 'angular-course'; @ViewChild(HeaderComponent) private header!: HeaderComponent; // 聲明周期鉤子: 組件及子組件視圖更新后調(diào)用,執(zhí)行一次 ngAfterViewInit(): void { // 調(diào)用子組件屬性 console.log(this.header.name); // 調(diào)用子組件函數(shù) this.header.printName(); } }
@Input和@Output
適用于父子關(guān)系組件
1、我們通過在header
組件中定義title
,來解耦title
組件中直接調(diào)用導(dǎo)致擴展復(fù)雜的問題
2、為title
組件中的title
屬性增加@Input()裝飾器: @Input() public title: string = '標(biāo)題';
3、為header組件新增title屬性并賦值: public title: string = '我是新標(biāo)題';
4、我們再header
組件的html
模板中這樣來使用title
組件: <app-title [title]="title"></app-title>
5、一起看看到現(xiàn)在的效果吧,界面雖然丑,但是下次使用組件時title
設(shè)置是不是方便一點呢?
6、以上步驟實現(xiàn)了父組件的數(shù)據(jù)傳遞到了子組件中,那么我們接著來看子組件的數(shù)據(jù)怎么傳遞到父組件中呢? 我們一起來用@Output()
裝飾器實現(xiàn)以下吧
7、在title
組件的ts類中增加titleChange
屬性: @Output() public titleChange = new EventEmitter();
8、在title
組件的ts類中定時派發(fā)數(shù)據(jù)
ngOnInit(): void { // 定時將子組件的數(shù)據(jù)進行派發(fā) setInterval(() => { this.titleChange.emit(this.title); }, 1500); }
9、現(xiàn)在我們來修改header父組件來接收派發(fā)來的數(shù)據(jù):
<app-title [title]="title" (titleChange)="onChildTitleChange($event)"> </app-title>
onChildTitleChange(value: any) { console.log('onChildTitleChange: >>', value); }
利用服務(wù)單利進行通信
適用于無直接關(guān)系組件
1、既然要通過服務(wù)來做通信,那我們就先創(chuàng)建一個服務(wù)吧: ng g s services/EventBus
,并且我們聲明了一個類型為Subject
的屬性來輔助通信
@Injectable({ providedIn: 'root', }) export class EventBusService { public eventBus: Subject<any> = new Subject(); constructor() {} }
2、我們?yōu)榱耸∈戮筒恢匦聞?chuàng)建組件了,因為我們的header
中的按鈕組件和title組件就符合沒有直接關(guān)系的組件。
3、改造一下我們的button
組件,并且添加點擊事件來觸發(fā)triggerEventBus
函數(shù)
export class ButtonComponent implements OnInit { public btnName: string = '按鈕'; constructor(public eventBusService: EventBusService) {} ngOnInit(): void {} public triggerEventBus(): void { this.eventBusService.eventBus.next('我是按鈕組件'); } }
4、在title
組件中模擬數(shù)據(jù)的獲取
export class TitleComponent implements OnInit { constructor(public eventBusService: EventBusService) {} ngOnInit(): void { this.eventBusService.eventBus.subscribe((value) => { console.log(value); }); } }
利用cookie、session或者localstorage進行通信
1、這個就很簡單了,我們還是用title
組件和button
組件來做演示,這次我們在title組件中將數(shù)據(jù)保存,在button
組件中獲取數(shù)據(jù)。我們僅演示localstorage
吧,其他都雷同的。
2、在title
組件的ngOnInit()
鉤子中保存title
到localstorage
中: window.localStorage.setItem('title', this.title);
3、在button組件中獲取數(shù)據(jù): const title = window.localStorage.getItem('title');
結(jié)語:
本篇我們介紹了Angular的組件通信,為我們拆分后的組件可以進行合理的通信提供了保障,我們到現(xiàn)在組件的使用都是通過引入標(biāo)簽的方式進行。