首頁 > web前端 > js教程 > 主體

Angular組件間怎麼進行互動?常用互動方法介紹

青灯夜游
發布: 2021-06-24 11:39:38
轉載
1917 人瀏覽過

元件之間的互動主要是在主從元件之間進行互動。那麼Angular元件之間怎麼進行互動?以下這篇文章跟大家介紹一下Angular組件間進行常用互動的方法。

Angular組件間怎麼進行互動?常用互動方法介紹

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

1、透過輸入型綁定把資料從父元件傳到子元件

child.component.ts

export class ChildComponent implements OnInit {
  @Input() hero: any;
  @Input('master') masterName: string;      // 第二个 @Input 为子组件的属性名 masterName 指定一个别名 master

  constructor() { }

  ngOnInit(): void {
  }

}
登入後複製

child.component.html

<div style="background-color: #749f84">
  <p>child works!</p>
  <h3>{{hero?.name}} says:</h3>
  <p>I, {{hero?.name}}, am at your service, {{masterName}}.</p>
</div>
登入後複製

parent.component.ts

export class ParentComponent implements OnInit {
  hero = {name: &#39;qxj&#39;}
  master = &#39;Master&#39;

  constructor() {
  }

  ngOnInit(): void {
  }

}
登入後複製

parent.component.html

<app-child [hero]="hero" [master]="master"></app-child>
登入後複製

#2、父元件監聽子元件的事件

##child.component. ts

export class ChildComponent implements OnInit {
  @Input()  name: string;
  @Output() voted = new EventEmitter<boolean>();
  didVote = false;

  vote(agreed: boolean) {
    this.voted.emit(agreed);
    this.didVote = true;
  }

  constructor() { }

  ngOnInit(): void {
  }

}
登入後複製

child.component.html

<h4>{{name}}</h4>
<button (click)="vote(true)"  [disabled]="didVote">Agree</button>
<button (click)="vote(false)" [disabled]="didVote">Disagree</button>
登入後複製

parent.component.ts

export class ParentComponent implements OnInit {
  agreed = 0
  disagreed = 0
  voters = [&#39;Narco&#39;, &#39;Celeritas&#39;, &#39;Bombasto&#39;]

  onVoted(agreed: boolean) {
    agreed ? this.agreed++ : this.disagreed++
  }

  constructor() {
  }

  ngOnInit(): void {
  }

}
登入後複製

parent.component.html

<h2>Should mankind colonize the Universe?</h2>
<h3>Agree: {{agreed}}, Disagree: {{disagreed}}</h3>
<app-child *ngFor="let voter of voters" [name]="voter" (voted)="onVoted($event)"></app-child>
登入後複製

Angular組件間怎麼進行互動?常用互動方法介紹

#3、父元件與子元件透過本地變數互動

#父元件不能使用資料綁定來讀取子元件的屬性或呼叫子組件的方法。但可以在父元件模板裡,新建一個本地變數來代表子元件,然後利用這個變數來讀取子元件的屬性和呼叫子元件的方法,如下例所示。

子元件

CountdownTimerComponent 進行倒數計時,歸零時發射一個飛彈。 startstop 方法負責控制時脈並在範本裡顯示倒數計時的狀態資訊。

child.component.ts

export class ChildComponent implements OnInit, OnDestroy {
  intervalId = 0
  message = &#39;&#39;
  seconds = 11

  clearTimer() {
    clearInterval(this.intervalId)
  }

  ngOnInit() {
    this.start()
  }

  ngOnDestroy() {
    this.clearTimer()
  }

  start() {
    this.countDown()
  }

  stop() {
    this.clearTimer()
    this.message = `Holding at T-${this.seconds} seconds`
  }

  private countDown() {
    this.clearTimer()
    this.intervalId = window.setInterval(() => {
      this.seconds -= 1
      if (this.seconds === 0) {
        this.message = &#39;Blast off!&#39;
      } else {
        if (this.seconds < 0) {
          this.seconds = 10
        } // reset
        this.message = `T-${this.seconds} seconds and counting`
      }
    }, 1000)
  }

}
登入後複製

child.component.html

<p>{{message}}</p>
登入後複製

parent.component.ts

export class ParentComponent implements OnInit {
  constructor() {
  }
  ngOnInit(): void {
  }
}
登入後複製

parent.component.html

<h3>Countdown to Liftoff (via local variable)</h3>
<button (click)="child.start()">Start</button>
<button (click)="child.stop()">Stop</button>
<div class="seconds">{{child.seconds}}</div>
<app-child #child></app-child>
登入後複製

countdown timer

4、父元件呼叫#@ViewChild()<span style="font-size: 16px;"></span> ##這個

本地變數

方法是個簡單便利的方法。但是它也有局限性,因為父組件-子組件的連接必須全部在父組件的模板中進行。父元件本身的程式碼對子元件沒有存取權。 如果父元件的

類別

需要讀取子元件的屬性值或呼叫子元件的方法,就不能使用本機變數方法。 當父元件

類別

需要這種存取時,可以把子元件當作 ViewChild,***注入***到父元件裡面。 countdown-parent.component.ts

import {AfterViewInit, Component, ViewChild} from &#39;@angular/core&#39;
import {ChildComponent} from &#39;../child/child.component&#39;

@Component({
  selector: &#39;app-parent-vc&#39;,
  template: `
    <h3>Countdown to Liftoff (via ViewChild)</h3>
    <button (click)="start()">Start</button>
    <button (click)="stop()">Stop</button>
    <div class="seconds">{{ seconds() }}</div>
    <app-child></app-child>
  `,
})
export class CountdownParentComponent implements AfterViewInit {

  @ViewChild(ChildComponent)
  private timerComponent: ChildComponent

  seconds() {
    return 0
  }

  ngAfterViewInit() {
    // Redefine `seconds()` to get from the `ChildComponent.seconds` ...
    // but wait a tick first to avoid one-time devMode
    // unidirectional-data-flow-violation error
    setTimeout(() => {
      this.seconds = () => this.timerComponent.seconds
    }, 0)
  }

  start() {
    this.timerComponent.start()
  }

  stop() {
    this.timerComponent.stop()
  }
}
登入後複製

更多程式相關知識,請造訪:

程式設計入門

! !

以上是Angular組件間怎麼進行互動?常用互動方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!