本篇文章帶大家繼續angular的學習,了解一下Angular中元件通訊的方法,希望對大家有幫助!
上一篇,我們講了 Angular 結合 NG-ZORRO 快速開發。前端開發,很大程度是組件化開發,永遠遠離不開組件之間的通訊。那麼,在 Angular
開發中,其元件之間的通訊是怎麼樣的呢? 【相關教學推薦:《angular教學》】
舉一反三,
Vue
和React
中大同小異
#本文純文字,比較枯燥。因為控制台列印的東西比較雞肋,所以就不配圖了,嗯~希望讀者跟著說明程式碼走一遍更容易吸收~
1. 父元件透過屬性傳遞值給子元件
相當於你自訂了一個屬性,透過元件的引入,將值傳遞給子元件。 Show you the CODE
。
<!-- parent.component.html --> <app-child [parentProp]="'My kid.'"></app-child>
在父元件中呼叫子元件,這裡命名一個 parentProp
的屬性。
// child.component.ts import { Component, OnInit, Input } from '@angular/core'; @Component({ selector: 'app-child', templateUrl: './child.component.html', styleUrls: ['./child.component.scss'] }) export class ChildComponent implements OnInit { // 输入装饰器 @Input() parentProp!: string; constructor() { } ngOnInit(): void { } }
子元件接受父元件傳入的變數 parentProp
,回填到頁面。
<!-- child.component.html --> <h1 id="Hello-nbsp-nbsp-parentProp-nbsp">Hello! {{ parentProp }}</h1>
2. 子元件透過Emitter 事件傳遞訊息給父元件
透過new EventEmitter()
將子元件的資料傳遞給父組件。
// child.component.ts import { Component, OnInit, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'app-child', templateUrl: './child.component.html', styleUrls: ['./child.component.scss'] }) export class ChildComponent implements OnInit { // 输出装饰器 @Output() private childSayHi = new EventEmitter() constructor() { } ngOnInit(): void { this.childSayHi.emit('My parents'); } }
透過 emit
通知父元件,父元件對事件進行監聽。
// parent.component.ts import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-communicate', templateUrl: './communicate.component.html', styleUrls: ['./communicate.component.scss'] }) export class CommunicateComponent implements OnInit { public msg:string = '' constructor() { } ngOnInit(): void { } fromChild(data: string) { // 这里使用异步 setTimeout(() => { this.msg = data }, 50) } }
在父元件中,我們對 child
元件來的資料進行監聽後,這裡採用了 setTimeout
的非同步操作。是因為我們在子元件中初始化後就進行了 emit
,這裡的非同步操作是防止 Race Condition
競爭出錯。
我們也得在元件中加入fromChild
這個方法,如下:
<!-- parent.component.html --> <h1 id="Hello-nbsp-nbsp-msg-nbsp">Hello! {{ msg }}</h1> <app-child (childSayHi)="fromChild($event)"></app-child>
3. 透過引用,父元件取得子元件的屬性與方法
我們透過操縱引用的方式,取得子元件對象,然後對其屬性和方法進行存取。
我們先設定子元件的示範內容:
// child.component.ts import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-child', templateUrl: './child.component.html', styleUrls: ['./child.component.scss'] }) export class ChildComponent implements OnInit { // 子组件的属性 public childMsg:string = 'Prop: message from child' constructor() { } ngOnInit(): void { } // 子组件方法 public childSayHi(): void { console.log('Method: I am your child.') } }
我們在父元件上設定子元件的引用識別#childComponent
:
<!-- parent.component.html --> <app-child #childComponent></app-child>
之後在javascript
檔案上呼叫:
import { Component, OnInit, ViewChild } from '@angular/core'; import { ChildComponent } from './components/child/child.component'; @Component({ selector: 'app-communicate', templateUrl: './communicate.component.html', styleUrls: ['./communicate.component.scss'] }) export class CommunicateComponent implements OnInit { @ViewChild('childComponent') childComponent!: ChildComponent; constructor() { } ngOnInit(): void { this.getChildPropAndMethod() } getChildPropAndMethod(): void { setTimeout(() => { console.log(this.childComponent.childMsg); // Prop: message from child this.childComponent.childSayHi(); // Method: I am your child. }, 50) } }
這種方法有個限制?,就是子屬性的修飾符需要是public
,當是protected
或private
的時候,會報錯。你可以將子組件的修飾符更改下嘗試。報錯的原因如下:
類型 | 使用範圍 |
---|---|
允許在累的內外被調用,作用範圍最廣 | |
允許在類別內以及繼承的子類別中使用,作用範圍適中 | |
允許在類別內部使用,作用範圍最窄 |
4.透過service 去變動
我們結合rxjs 來示範。
rxjs 是使用 Observables 的響應式程式設計的函式庫,它使編寫非同步或基於回呼的程式碼更容易。
後會有一篇文章記錄我們先來建立一個名為rxjs
,請期待
parent-and-child 的服務。
// parent-and-child.service.ts import { Injectable } from '@angular/core'; import { BehaviorSubject, Observable } from 'rxjs'; // BehaviorSubject 有实时的作用,获取最新值 @Injectable({ providedIn: 'root' }) export class ParentAndChildService { private subject$: BehaviorSubject<any> = new BehaviorSubject(null) constructor() { } // 将其变成可观察 getMessage(): Observable<any> { return this.subject$.asObservable() } setMessage(msg: string) { this.subject$.next(msg); } }接著,我們在父子元件中引用,它們的資訊是共享的。
// parent.component.ts import { Component, OnDestroy, OnInit } from '@angular/core'; // 引入服务 import { ParentAndChildService } from 'src/app/services/parent-and-child.service'; import { Subject } from 'rxjs' import { takeUntil } from 'rxjs/operators' @Component({ selector: 'app-communicate', templateUrl: './communicate.component.html', styleUrls: ['./communicate.component.scss'] }) export class CommunicateComponent implements OnInit, OnDestroy { unsubscribe$: Subject<boolean> = new Subject(); constructor( private readonly parentAndChildService: ParentAndChildService ) { } ngOnInit(): void { this.parentAndChildService.getMessage() .pipe( takeUntil(this.unsubscribe$) ) .subscribe({ next: (msg: any) => { console.log('Parent: ' + msg); // 刚进来打印 Parent: null // 一秒后打印 Parent: Jimmy } }); setTimeout(() => { this.parentAndChildService.setMessage('Jimmy'); }, 1000) } ngOnDestroy() { // 取消订阅 this.unsubscribe$.next(true); this.unsubscribe$.complete(); } }
import { Component, OnInit } from '@angular/core'; import { ParentAndChildService } from 'src/app/services/parent-and-child.service'; @Component({ selector: 'app-child', templateUrl: './child.component.html', styleUrls: ['./child.component.scss'] }) export class ChildComponent implements OnInit { constructor( private parentAndChildService: ParentAndChildService ) { } // 为了更好理解,这里我移除了父组件的 Subject ngOnInit(): void { this.parentAndChildService.getMessage() .subscribe({ next: (msg: any) => { console.log('Child: '+msg); // 刚进来打印 Child: null // 一秒后打印 Child: Jimmy } }) } }在父元件中,我們一秒鐘之後會改變值。所以在父子元件中,一進來就會印出
msg 的初始值
null,然後過了一秒鐘之後,就會印出更改的值
Jimmy# 。同理,如果你在子元件中對服務的訊息,在子元件列印相關的值的同時,在父元件也會列印。
程式設計入門! !
以上是聊聊Angular中組件之間怎麼通信的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本篇文章继续Angular的学习,带大家了解一下Angular中的元数据和装饰器,简单了解一下他们的用法,希望对大家有所帮助!

本篇文章带大家深入了解一下angular的状态管理器NgRx,介绍一下NgRx的使用方法,希望对大家有所帮助!

angular中怎么使用monaco-editor?下面本篇文章记录下最近的一次业务中用到的 monaco-editor 在 angular 中的使用,希望对大家有所帮助!

本篇文章给大家分享一个Angular实战,了解一下angualr 结合 ng-zorro 如何快速开发一个后台系统,希望对大家有所帮助!

怎么自定义angular-datetime-picker格式?下面本篇文章聊聊自定义格式的方法,希望对大家有所帮助!

Angular项目过大,怎么合理拆分它?下面本篇文章给大家介绍一下合理拆分Angular项目的方法,希望对大家有所帮助!

本篇文章带大家了解一下Angular中的独立组件,看看怎么在Angular中创建一个独立组件,怎么在独立组件中导入已有的模块,希望对大家有所帮助!

Angular Route中怎么提前获取数据?下面本篇文章给大家介绍一下从 Angular Route 中提前获取数据的方法,希望对大家有所帮助!


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。