今天就跟大家聊聊有關(guān)Angular中父子組件間如何進行通信,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)建站專注于孟村網(wǎng)站建設(shè)服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供孟村營銷型網(wǎng)站建設(shè),孟村網(wǎng)站制作、孟村網(wǎng)頁設(shè)計、孟村網(wǎng)站官網(wǎng)定制、成都微信小程序服務,打造孟村網(wǎng)絡公司原創(chuàng)品牌,更為您提供孟村網(wǎng)站排名全網(wǎng)營銷落地服務。
通過Input和Ouput傳值
父組件:html和ts
<app-liftcycle [name]="name" (changeName)="changeName($event)"></app-liftcycle>
public name: string = "jack"; public changeName(value: string) { this.name = value; }
子組件:html和ts
<div (click)="emit()">{{name}}</div>
import { Component, Input, EventEmitter, Output } from '@angular/core'; @Input() name: string; @Output() changeName: EventEmitter<string> = new EventEmitter<string>(); public emit() { this.changeName.emit("修改name屬性"); }
通過setter監(jiān)聽屬性的變化
父組件同上,子組件:
private _name: string = ""; @Input() public get name(): string { return this._name; } public set name(value: string) { this._name = value + "定義結(jié)構(gòu)"; }
通過ngOnChanges鉤子函數(shù)監(jiān)聽輸入屬性的變化
ngOnChanges在監(jiān)聽多個屬性的時候,要比setter的方式簡便一些。
@Input() name: string; ngOnChanges(changes: SimpleChanges): void { (({name}) => { console.log(name.currentValue,name.previousValue); })(changes); }
父組件html中通過模板變量調(diào)用子組件的方法和屬性。
模板變量獲取了子組件的一個引用。 父組件:
<app-liftcycle #child></app-liftcycle> <button (click)="child.childFn()">按鈕</button>
子組件:
public childFn() { console.log("通過模板變量調(diào)用子組件中的方法"); }
父組件通過ViewChild獲取子組件實例
<app-liftcycle [name]="name" (changeName)="changeName($event)" #child></app-liftcycle> <button (click)="childFn()">childFn</button>
@ViewChild("child") child: LiftcycleComponent; public childFn(): void { this.child.childFn(); }
通過service進行通信
service:
import { Subject } from 'rxjs'; import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class CommunService { constructor() {} public commun = new Subject<string>(); communSend() { this.commun.next("send"); } }
父組件:
constructor(private commun: CommunService) { } public send(): void { this.commun.communSend(); }
子組件:
constructor(private commun: CommunService) { this.commun.commun.subscribe((value) => {console.log(value)}); }
父組件傳遞方法
父組件通過屬性傳遞給子組件方法,子組件進行調(diào)用,一般不推薦,React采用這種通信方式。 可能是基于this的綁定錯綜復雜,所以angular不太推薦。React Hooks的出現(xiàn)也有一部分原因 是class類的this錯綜復雜。 父組件:
<app-liftcycle [send]="send.bind(this)"></app-liftcycle>
public name: string = "jack"; public send(): void { console.log(this.name); }
子組件:
<button (click)="childSend()">childSend</button>
@Input() send: Function; public childSend() { this.send(); }
看完上述內(nèi)容,你們對Angular中父子組件間如何進行通信有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當前名稱:Angular中父子組件間如何進行通信
本文網(wǎng)址:http://www.rwnh.cn/article14/jsdjde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、品牌網(wǎng)站建設(shè)、Google、企業(yè)建站、營銷型網(wǎng)站建設(shè)、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)