欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網
          最全最豐富的資訊網站

          Angular組件怎么進行通信?父子組件通信的2種方法

          本篇文章帶大家了解一下Angular中的組件通信,介紹一下父子組件間通信、無直接關系組件間通信的方法。

          Angular組件怎么進行通信?父子組件通信的2種方法

          在實際的應用中我們的組件將會以樹形的結構進行關聯(lián),所以組件間的關系主要就是:

          • 父子關系

          • 兄弟關系

          • 無直接關系

          【相關教程推薦:《angular教程》】

          準備一下我們的環(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 = '標題';    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 {} }

          直接調用

          適用于父子關系組件,注意點是直接調用使得父子組件的耦合性變高,要明確使用確實需要直接調用。

          1、將我們的header組件掛載到app中,使得app和header之間形成父子組件關系

          2、使用#為我們的組件起一個名稱: <app-header #header></app-header>

          3、現(xiàn)在我們的header組件還很空,我們擴展一下,要不然調用什么呢?

          export class HeaderComponent implements OnInit {   public name: string = 'HeaderComponent';    printName(): void {     console.log('component name is', this.name);   } }

          4、組件擴展好以后我們就可以在父組件app中調用子組件header中的屬性和函數(shù)了

          <app-header #header></app-header> <p>   調用子組件屬性: {{ header.name }}   <button (click)="header.printName()">調用子組件函數(shù)</button> </p>

          5、第4步是在父組件的html模板中進行操作,有時候我們還需要在父組件的ts類中對子組件進行操作,我們接下來接著演示。

          6、我們需要用到一個新的裝飾器@ViewChild(Component)

          export class AppComponent {   title = 'angular-course';    @ViewChild(HeaderComponent)   private header!: HeaderComponent;  	// 聲明周期鉤子: 組件及子組件視圖更新后調用,執(zhí)行一次   ngAfterViewInit(): void {     // 調用子組件屬性     console.log(this.header.name);     // 調用子組件函數(shù)     this.header.printName();   } }

          @Input和@Output

          適用于父子關系組件

          1、我們通過在header組件中定義title,來解耦title組件中直接調用導致擴展復雜的問題

          2、為title組件中的title屬性增加@Input()裝飾器: @Input() public title: string = '標題';

          3、為header組件新增title屬性并賦值: public title: string = '我是新標題';

          4、我們再header組件的html模板中這樣來使用title組件: <app-title [title]="title"></app-title>

          5、一起看看到現(xiàn)在的效果吧,界面雖然丑,但是下次使用組件時title設置是不是方便一點呢?

          Angular組件怎么進行通信?父子組件通信的2種方法

          6、以上步驟實現(xiàn)了父組件的數(shù)據傳遞到了子組件中,那么我們接著來看子組件的數(shù)據怎么傳遞到父組件中呢? 我們一起來用@Output()裝飾器實現(xiàn)以下吧

          7、在title組件的ts類中增加titleChange屬性: @Output() public titleChange = new EventEmitter();

          8、在title組件的ts類中定時派發(fā)數(shù)據

          ngOnInit(): void {   // 定時將子組件的數(shù)據進行派發(fā)   setInterval(() => {   	this.titleChange.emit(this.title); 	}, 1500); }

          9、現(xiàn)在我們來修改header父組件來接收派發(fā)來的數(shù)據:

          <app-title  	[title]="title"    (titleChange)="onChildTitleChange($event)"> </app-title>
          onChildTitleChange(value: any) { 	console.log('onChildTitleChange: >>', value); }

          利用服務單利進行通信

          適用于無直接關系組件

          Angular組件怎么進行通信?父子組件通信的2種方法

          1、既然要通過服務來做通信,那我們就先創(chuàng)建一個服務吧: 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組件就符合沒有直接關系的組件。

          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ù)據的獲取

          export class TitleComponent implements OnInit {    constructor(public eventBusService: EventBusService) {}    ngOnInit(): void {     this.eventBusService.eventBus.subscribe((value) => {       console.log(value);     });   } }

          利用cookie、session或者localstorage進行通信

          Angular組件怎么進行通信?父子組件通信的2種方法

          1、這個就很簡單了,我們還是用title組件和button組件來做演示,這次我們在title組件中將數(shù)據保存,在button組件中獲取數(shù)據。我們僅演示localstorage吧,其他都雷同的。

          2、在title組件的ngOnInit()鉤子中保存titlelocalstorage中: window.localStorage.setItem('title', this.title);

          3、在button組件中獲取數(shù)據: const title = window.localStorage.getItem('title');

          結語:

          本篇我們介紹了Angular的組件通信,為我們拆分后的組件可以進行合理的通信提供了保障,我們到現(xiàn)在組件的使用都是通過引入標簽的方式進行。

          贊(0)
          分享到: 更多 (0)
          網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號