<del id="d4fwx"><form id="d4fwx"></form></del>
      <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

            <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
          • Angular中父子組件間如何進(jìn)行通信

            今天就跟大家聊聊有關(guān)Angular中父子組件間如何進(jìn)行通信,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

            創(chuàng)新互聯(lián)建站專注于孟村網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供孟村營銷型網(wǎng)站建設(shè),孟村網(wǎng)站制作、孟村網(wǎng)頁設(shè)計(jì)、孟村網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造孟村網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供孟村網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

            通過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)聽多個(gè)屬性的時(shí)候,要比setter的方式簡便一些。

            @Input() name: string;
            ngOnChanges(changes: SimpleChanges): void {
                (({name}) => {
                    console.log(name.currentValue,name.previousValue);
                })(changes);
            }

            父組件html中通過模板變量調(diào)用子組件的方法和屬性。

            模板變量獲取了子組件的一個(gè)引用。 父組件:

            <app-liftcycle #child></app-liftcycle>
            <button (click)="child.childFn()">按鈕</button>

            子組件:

            public childFn() {
                console.log("通過模板變量調(diào)用子組件中的方法");
            }

            父組件通過ViewChild獲取子組件實(shí)例

            <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進(jìn)行通信

            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)});
            }

            父組件傳遞方法

            父組件通過屬性傳遞給子組件方法,子組件進(jìn)行調(diào)用,一般不推薦,React采用這種通信方式。 可能是基于this的綁定錯(cuò)綜復(fù)雜,所以angular不太推薦。React Hooks的出現(xiàn)也有一部分原因 是class類的this錯(cuò)綜復(fù)雜。 父組件:

            <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中父子組件間如何進(jìn)行通信有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

            當(dāng)前名稱:Angular中父子組件間如何進(jìn)行通信
            本文網(wǎng)址:http://www.jbt999.com/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)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

            綿陽服務(wù)器托管

              <del id="d4fwx"><form id="d4fwx"></form></del>
              <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

                    <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
                  • 天天日天天躁 | 在线免费观看黄片视频 | 奶大灬舒服灬一进一出三区 | 成人黄色录像 | 被操的好爽|