<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
        問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
        當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

        Angular2 父子組件通信方式的示例

        來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:20:58
        文檔

        Angular2 父子組件通信方式的示例

        Angular2 父子組件通信方式的示例:Angular2官方文檔對(duì)組件交互這塊有詳細(xì)的介紹-->文檔--組件之間的交互。按文檔介紹,組件間交互的方式一共有4種,包括: 通過(guò)輸入型綁定把數(shù)據(jù)從父組件傳到子組件(@Input decoration);子組件暴露一個(gè)EventEmitter屬性(@Output dec
        推薦度:
        導(dǎo)讀Angular2 父子組件通信方式的示例:Angular2官方文檔對(duì)組件交互這塊有詳細(xì)的介紹-->文檔--組件之間的交互。按文檔介紹,組件間交互的方式一共有4種,包括: 通過(guò)輸入型綁定把數(shù)據(jù)從父組件傳到子組件(@Input decoration);子組件暴露一個(gè)EventEmitter屬性(@Output dec

        Angular2官方文檔對(duì)組件交互這塊有詳細(xì)的介紹-->文檔--組件之間的交互。按文檔介紹,組件間交互的方式一共有4種,包括:

        1. 通過(guò)輸入型綁定把數(shù)據(jù)從父組件傳到子組件(@Input decoration);子組件暴露一個(gè)EventEmitter屬性(@Output decoration),當(dāng)事件發(fā)生時(shí),利用該屬性emits向父組件發(fā)射事件。
        2. 父組件與子組件通過(guò)本地變量互動(dòng)。(# var)
        3. 父組件調(diào)用@ViewChild。
        4. 父組件和子組件通過(guò)服務(wù)來(lái)通訊。

        我在這里只總結(jié)、詳細(xì)介紹3種我在項(xiàng)目中使用過(guò)的方法,看完本文大概能做到如下的效果:


        創(chuàng)建項(xiàng)目,項(xiàng)目結(jié)構(gòu)如下:


        通過(guò)@Input、@Output裝飾器進(jìn)行父、子組件間的通信

        @Input:該屬性綁定用于父組件向子組件傳遞數(shù)據(jù)。子組件可以通過(guò)以下兩種方法截取屬性的變更:

        1. 使用一個(gè)輸入屬性的setter,以攔截父組件中值得變化。
        2. 通過(guò)ngOnchanges()來(lái)截聽(tīng)輸入屬性值的變化。

        @Output:該數(shù)據(jù)綁定用于子組件向父組件傳遞數(shù)據(jù)和事件。

        <!--parent.component.html-->
        <div style="width: 1000px;margin: auto">
        <div class="card" style="width: 500px;float: left">
         <div class="card-header">
         父組件
         </div>
         <div class="card-body">
         <h5 class="card-title">父組件</h5>
         <div class="form-group">
         <label for="input">父組件輸入:</label>
         <input type="text"
         class="form-control"
         id="input"
         placeholder="Input something"
         [(ngModel)]="parentPrint" 
         >
         <label for="output">父組件
        輸出:</label> <input type="text" class="form-control" id="output" placeholder="Output something" [(ngModel)]="contentFromChild" > </div> </div> </div> <app-child [fromParent]="parentPrint" (fromChild)="fromChild($event)" ></app-child> </div>
        <!--child.component.html-->
        <div class="card" style="width: 500px;">
         <div class="card-header">
         子組件
         </div>
         <div class="card-body">
         <h5 class="card-title">子組件</h5>
         <div class="form-group">
         <label for="input">子組件輸入:</label>
         <input type="text"
         class="form-control"
         id="input"
         placeholder="Input something"
         [(ngModel)]="contentFromChild"
         >
         <label for="output">子組件
        輸出:</label> <input type="text" class="form-control" id="output" placeholder="Output something" [(ngModel)]="fromParent" > </div> <button class="btn btn-primary" (click)="clickChild()">Output方式</button> </div> </div>

        效果如下:(1、父組件輸入,子組件可同步輸出;2、子組件輸入需要(3、)點(diǎn)擊按鈕觸發(fā)發(fā)射事件,將數(shù)據(jù)傳送給父組件。)


        @Input:父組件輸入的同時(shí),子組件能同步獲取數(shù)據(jù)進(jìn)行顯示。核心代碼如下:

        //父組件
        parentPrint: any; //ts中,聲明一個(gè)變量
        [(ngModel)]="parentPrint" //html中,綁定變量,獲取用戶(hù)輸入
        //html中,將數(shù)據(jù)傳給子組件
        <app-child [fromParent]="parentPrint"></app-child> 
        //子組件
        @Input() fromParent; //ts中,用于直接接收從父組件獲取的數(shù)據(jù)
        [(ngModel)]="fromParent" //html中,用于顯示數(shù)據(jù)

        通過(guò)setter截聽(tīng)輸入屬性值的變化,在子組件中聲明一個(gè)私有變量來(lái)獲取父組件傳遞過(guò)來(lái)的數(shù)據(jù),從而屏蔽上層獲取下層信息。(簡(jiǎn)單一點(diǎn)就是不讓父組件知道子組件用什么東西去接收傳過(guò)來(lái)的數(shù)據(jù))通過(guò)這種方法也可以獲得同樣的效果。

        //子組件
         private _fromParent: any; //私有變量,通過(guò)setter獲取父組件的數(shù)據(jù)
        @Input() //通過(guò)setter獲取父組件的數(shù)據(jù)
         set fromParent(fromParent: any) {
         this._fromParent = fromParent;
         }
         get fromParent(): any {
         return this._fromParent;
         }
        

        @Output:父組件接收子組件的數(shù)據(jù)時(shí),子組件暴露一個(gè)EventEmitter屬性,當(dāng)事件發(fā)生時(shí),子組件利用該屬性emits(向上彈射)事件。父組件綁定到這個(gè)事件屬性,并在事件發(fā)生時(shí)作出回應(yīng)。核心代碼如下:

        //子組件
        @Output() fromChild = new EventEmitter<any>(); //暴露一個(gè)
        輸出屬性 <button class="btn btn-primary" (click)="clickChild()">Output方式</button> //觸發(fā)發(fā)射函數(shù),將數(shù)據(jù)發(fā)送給父組件 clickChild() { console.log('click child' , this.contentFromChild); this.fromChild.emit(this.contentFromChild); }
        //父組件
        [(ngModel)]="contentFromChild" //綁定
        輸出子組件的數(shù)據(jù) //使用子組件,綁定事件屬性 <app-child [fromParent]="parentPrint" (fromChild)="fromChild($event)" ></app-child> //事件處理函數(shù) fromChild(event) { console.log(event); this.contentFromChild = event; }

        父組件通過(guò)調(diào)用@ViewChild()來(lái)獲取子組件的數(shù)據(jù)

        如果父組件的類(lèi)需要讀取子組件的屬性和值或調(diào)用子組件的方法時(shí),就可以把子組件作為ViewChild,注入到父組件里面。ViewChild顧名思義就是可以看見(jiàn)子組件里面的屬性和方法。

        <!--parent.component.html-->
        <div style="width: 1000px;margin: auto">
        <div class="card" style="width: 500px;float: left">
         <div class="card-header">
         父組件
         </div>
         <div class="card-body">
         <h5 class="card-title">父組件</h5>
         <div class="form-group">
         <label for="viewoutput">ViewChild父組件
        輸出:</label> <input type="text" class="form-control" id="viewoutput" placeholder="ViewChild父組件輸出" [(ngModel)]="viewOutput" > </div> <button class="btn btn-primary" (click)="clickView()">ViewChild方式</button> </div> </div> <app-child></app-child> </div>
        <!--child.component.html-->
        <div class="card" style="width: 500px;">
         <div class="card-header">
         子組件
         </div>
         <div class="card-body">
         <h5 class="card-title">子組件</h5>
         <div class="form-group">
         <label for="input">子組件輸入:</label>
         <input type="text"
         class="form-control"
         id="input"
         placeholder="Input something"
         [(ngModel)]="contentFromChild"
         >
         </div>
         </div>
        </div>
        
        

        效果如下:

        父組件核心代碼:

        //ts
        @ViewChild(ChildComponent) // 使用viewChild導(dǎo)入引用
        private childComponent: ChildComponent; // 將子組件注入到私有屬性
        //獲取子組件數(shù)據(jù)并顯示
        clickView() {
         //直接獲取子組件的屬性
         this.viewOutput = this.childComponent.contentFromChild;
         }
        //html
        [(ngModel)]="viewOutput"
         <button class="btn btn-primary" (click)="clickView()">ViewChild方式</button>
        
        

        父組件和子組件通過(guò)服務(wù)來(lái)通訊

        父組件和它的子組件共享同一個(gè)服務(wù),利用該服務(wù)在家庭內(nèi)部實(shí)現(xiàn)雙向通訊。

        <!--parent.component.html-->
        <div style="width: 1000px;margin: auto">
        <div class="card" style="width: 500px;float: left">
         <div class="card-header">
         父組件
         </div>
         <div class="card-body">
         <h5 class="card-title">父組件</h5>
         <div class="form-group">
         <label for="serviceoutput">父組件服務(wù)輸入:</label>
         <input type="text"
         class="form-control"
         id="serviceoutput"
         placeholder="服務(wù)輸入"
         [(ngModel)]="serviceInput"
         >
         </div>
         <button class="btn btn-primary" (click)="clickService()">Service方式</button>
         </div>
        </div>
        <app-child></app-child>
        </div>
        
        <!--child.component.html-->
        <div class="card" style="width: 500px;">
         <div class="card-header">
         子組件
         </div>
         <div class="card-body">
         <h5 class="card-title">子組件</h5>
         <div class="form-group">
         <label for="serviceoutput">子組件服務(wù)輸入:</label>
         <input type="text"
         class="form-control"
         id="serviceoutput"
         placeholder="服務(wù)輸入"
         [(ngModel)]="serviceInput"
         >
         </div>
         <button class="btn btn-primary" (click)="clickService()">Service方式</button>
         </div>
        </div>
        
        
        //服務(wù)
        //meditor.service.ts
        import {Injectable} from '@angular/core';
        import {Subject} from 'rxjs/Subject';
        import {Observable} from 'rxjs/Observable';
        
        @Injectable()
        export class MeditorService {
         private subject = new Subject<MeditorMsg>();
         constructor() {}
         // 獲取訂閱者
         public getObservable(): Observable<MeditorMsg> {
         return this.subject.asObservable();
         }
         // 推送信息
         public push(msg: MeditorMsg) {
         this.subject.next(msg);
         }
        }
        // 中間者信息
        export interface MeditorMsg {
         id: string;
         body: any;
        }
        
        

        效果如下:


        父子組件的核心代碼類(lèi)似,在構(gòu)造函數(shù)中將該服務(wù)實(shí)例注入到自身,父子組件都有一個(gè)唯一的id。無(wú)論是父組件還是子組件調(diào)用push()方法推送數(shù)據(jù),雙方都能接收到數(shù)據(jù),這時(shí)候就要根據(jù)id來(lái)判斷是要父組件使用數(shù)據(jù)還是子組件使用數(shù)據(jù)。核心代碼如下:

        subscription: Subscription = null; //初始化一個(gè)訂閱對(duì)象
        //子組件構(gòu)造函數(shù),用于監(jiān)聽(tīng)數(shù)據(jù)推送
        constructor(
         private meditor: MeditorService
         ) {
         this.subscription = meditor.getObservable().subscribe(
         msg => {
         console.log(msg);
         if (msg.id === 'parent') { //id為parent,獲取父組件數(shù)據(jù)
         this.serviceInput = msg.body;
         }
         }
         );
         }
        // 子組件將數(shù)據(jù)推送到中間著,給訂閱者
        clickService() {
         this.meditor.push({id: 'parent', body: this.serviceInput});
         }
        //父組件構(gòu)造函數(shù),用于監(jiān)聽(tīng)數(shù)據(jù)推送
        constructor(
         private meditor: MeditorService
         ) {
         this.subscription = meditor.getObservable().subscribe(
         msg => {
         console.log(msg);
         if (msg.id === 'child') { //id為child,獲取子組件數(shù)據(jù)
         this.serviceInput = msg.body;
         }
         }
         );
         }
        // 父組件將數(shù)據(jù)推送到中間著,給訂閱者
        clickService() {
         this.meditor.push({id: 'parent', body: this.serviceInput});
         }
        

        我上面寫(xiě)的還不是很完善,就是在生命周期結(jié)束前,也就是在onDestroy周期中,要取消訂閱。

        以上,就是最近在使用的組件交互的總結(jié)。個(gè)人覺(jué)得通過(guò)服務(wù)來(lái)交互的可擴(kuò)展性更強(qiáng)。例如,我們項(xiàng)目中用到了一個(gè)動(dòng)態(tài)顯示的側(cè)欄,不同時(shí)期點(diǎn)擊顯示側(cè)欄要顯示不同的東西。這個(gè)時(shí)候把側(cè)欄作為父組件,子組件作為消息的一部分傳遞給父組件,父組件根據(jù)子組件名動(dòng)態(tài)生成模板,顯示在側(cè)欄上面。說(shuō)了這么多廢話大概就是下圖的意思:


        最后附上demo源碼:父子組件交互demo

        聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        Angular2 父子組件通信方式的示例

        Angular2 父子組件通信方式的示例:Angular2官方文檔對(duì)組件交互這塊有詳細(xì)的介紹-->文檔--組件之間的交互。按文檔介紹,組件間交互的方式一共有4種,包括: 通過(guò)輸入型綁定把數(shù)據(jù)從父組件傳到子組件(@Input decoration);子組件暴露一個(gè)EventEmitter屬性(@Output dec
        推薦度:
        標(biāo)簽: 通信 組件 angular2
        • 熱門(mén)焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門(mén)推薦

        專(zhuān)題
        Top
        主站蜘蛛池模板: 精品免费久久久久国产一区| 日韩精品无码一区二区三区免费 | 国产AV无码专区亚洲AV琪琪| 免费h成人黄漫画嘿咻破解版| 国产一级a毛一级a看免费视频| 亚洲欧洲在线播放| 免费**毛片在线播放直播| 免费观看男人吊女人视频| 亚洲国产精品成人AV在线| 亚洲高清国产AV拍精品青青草原| 久久WWW免费人成人片| 中文字幕无码免费久久9一区9 | 免费在线看污视频| 亚洲精品无码不卡在线播放| 亚洲乱码中文字幕久久孕妇黑人| 99精品国产免费久久久久久下载| 一本久到久久亚洲综合| 99热这里有免费国产精品| 美女裸免费观看网站| 亚洲AV色吊丝无码| 亚洲精品无码MV在线观看| 国产视频精品免费| 中文免费观看视频网站| 一级做a爰片久久免费| 亚洲午夜一区二区三区| 久久精品视频亚洲| 亚洲成a人在线看天堂无码| 91免费资源网站入口| 无码av免费一区二区三区试看| 特级毛片A级毛片100免费播放| 亚洲人成网站在线观看播放动漫| 亚洲综合伊人久久综合| 日韩亚洲国产二区| 嫩草视频在线免费观看| 69天堂人成无码麻豆免费视频| 久久精品成人免费看| 国产V片在线播放免费无码 | 亚洲AV无码专区亚洲AV桃| 亚洲综合小说久久另类区| 亚洲成色在线综合网站| 亚洲天堂中文字幕在线|