欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Angular中組件間通訊的實(shí)現(xiàn)方法

這篇文章主要介紹Angular中組件間通訊的實(shí)現(xiàn)方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

在宿松等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供做網(wǎng)站、成都做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),網(wǎng)絡(luò)營銷推廣,外貿(mào)網(wǎng)站制作,宿松網(wǎng)站建設(shè)費(fèi)用合理。

Angular 組件間的通訊


組件間三種典型關(guān)系:
Angular中組件間通訊的實(shí)現(xiàn)方法

  • 父好組件之間的交互(@Input/@Output/模板變量/@ViewChild)

  • 非父子組件之間的交互(Service/localStorage)

  • 還可以可以利用 Session、 路由參數(shù)來進(jìn)行通訊等

相關(guān)教程推薦:《angular教程》

父子組件之間交互

子組件編寫

  • child.component.ts

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {

  private _childTitle = '我是子組件';

  @Input()
  set childTitle(childTitle: string) {
    this._childTitle = childTitle;
  }

  get childTitle(): string {
    return this._childTitle;
  }

  @Output()
  messageEvent: EventEmitter<string> = new EventEmitter<string>();

  constructor() { }

  ngOnInit(): void {

  }

  sendMessage(): void {
    this.messageEvent.emit('我是子組件');
  }

  childFunction(): void {
    console.log('子組件的名字是:' + this.childTitle);
  }

}
  • child.component.html

<div class="panel panel-primary">
  <div class="panel-heading">{{childTitle}}</div>
  <div class="panel-body">
      <button (click)="sendMessage()" class="btn btn-success">給父組件發(fā)消息</button>
  </div>
</div>

父組件

  • parent-and-child.component.ts

@Component({
  selector: 'app-parent-and-child',
  templateUrl: './parent-and-child.component.html',
  styleUrls: ['./parent-and-child.component.css']
})
export class ParentAndChildComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

  doSomething(event: any): void {
    alert(event);
  }

}
  • parent-and-child.component.html

<div class="panel panel-primary">
  <div class="panel-heading">父組件</div>
  <div class="panel-body">
    <app-child #child (messageEvent) = "doSomething($event)"></app-child>
    <button (click)="child.childFunction()" class="btn btn-success">調(diào)用子組件的方法</button>
  </div>
</div>

@Input 屬性綁定是單向的,父組件的屬性變化會(huì)影響子組件的屬性變化, 子組件的屬性變化不會(huì)反過來影響父組件的的屬性變化。

不過,可以利用 @Input() 和 @Output() 實(shí)現(xiàn)屬性的雙向綁定。

@Input()
value: string;
@Output()
valueChange: EventEmitter<any> = new EventEmitter();

// 實(shí)現(xiàn)雙向綁定
<input [(value)] = "newValue"></input>

注意:使用 [()] 進(jìn)行雙向綁定時(shí),輸出屬性名必須是輸入屬性名與 Change 組成, 形如: xxxChange。

非父子組件之間交互

使用 Service 進(jìn)行交互

  • event-bus.service.ts

/**
 * 用于充當(dāng)事件總線
 */
@Injectable()
export class EventBusService {

  evnetBus: Subject<string> = new Subject<string>();

  constructor() { }
}
  • child1.component.ts

@Component({
  selector: 'app-child1',
  templateUrl: './child1.component.html',
  styleUrls: ['./child1.component.css']
})
export class Child1Component implements OnInit {

  constructor(private eventBusService: EventBusService) { }

  ngOnInit(): void {
  }

  triggerEventBus(): void {
    this.eventBusService.evnetBus.next('child1 觸發(fā)的事件');
  }
}
  • child1.component.html

<div class="panel panel-primary">
  <div class="panel-heading">child1 組件</div>
  <div class="panel-body">
    <button (click)="triggerEventBus()" class="btn btn-success">觸發(fā)事件</button>
  </div>
</div>
  • child2.component.ts

@Component({
  selector: 'app-child2',
  templateUrl: './child2.component.html',
  styleUrls: ['./child2.component.css']
})
export class Child2Component implements OnInit {

  events: Array<string> = new Array<string>();

  constructor(private eventBusService: EventBusService) { }

  ngOnInit(): void {
    this.listenerEvent();
  }

  listenerEvent(): void {
    this.eventBusService.evnetBus.subscribe( value => {
      this.events.push(value);
    });
  }
}
  • child2.component.html

<div class="panel panel-primary">
  <div class="panel-heading">child2 組件</div>
  <div class="panel-body">
     <p *ngFor="let event of events">{{event}}</p>
  </div>
</div>
  • brother.component.ts

@Component({
  selector: 'app-brother',
  templateUrl: './brother.component.html',
  styleUrls: ['./brother.component.css']
})
export class BrotherComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

}
  • brother.component.html

<div class="panel panel-primary">
  <div class="panel-heading">第二種:沒有父子關(guān)系的組件間通訊</div>
  <div class="panel-body">
    <app-child1></app-child1>
    <app-child2></app-child2>
  </div>
</div>

使用 localStorage 進(jìn)行交互

  • local-child1.component.ts

@Component({
  selector: 'app-local-child1',
  templateUrl: './local-child1.component.html',
  styleUrls: ['./local-child1.component.css']
})
export class LocalChild1Component implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

  writeData(): void {
    window.localStorage.setItem('message', JSON.stringify({name: 'star', age: 22}));
  }

}
  • local-child1.component.html

<div class="panel panel-primary">
  <div class="panel-heading"> LocalChild1 組件</div>
  <div class="panel-body">
     <button class="btn btn-success" (click)="writeData()">寫入數(shù)據(jù)</button>
  </div>
</div>
  • local-child2.component.ts

@Component({
  selector: 'app-local-child2',
  templateUrl: './local-child2.component.html',
  styleUrls: ['./local-child2.component.css']
})
export class LocalChild2Component implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

  readData(): void {
    const dataStr = window.localStorage.getItem('message');
    const data = JSON.parse(dataStr);
    console.log('name:' + data.name, 'age:' + data.age);
  }

}
  • local-child2.component.html

<div class="panel panel-primary">
  <div class="panel-heading">LocalChild2 組件</div>
  <div class="panel-body">
    <button class="btn btn-success" (click)="readData()">讀取數(shù)據(jù)</button>
  </div>
</div>
  • local-storage.component.ts

@Component({
  selector: 'app-local-storage',
  templateUrl: './local-storage.component.html',
  styleUrls: ['./local-storage.component.css']
})
export class LocalStorageComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

}
  • local-storage.component.html

<div class="panel panel-primary">
  <div class="panel-heading">第三種方案:利用 localStorge 通訊</div>
  <div class="panel-body">
    <app-local-child1></app-local-child1>
    <app-local-child2></app-local-child2>
  </div>
</div>

最后,關(guān)于使用 Session、路由參數(shù)實(shí)現(xiàn)數(shù)據(jù)交互的方式,這里就不演示了。

以上是“Angular中組件間通訊的實(shí)現(xiàn)方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站標(biāo)題:Angular中組件間通訊的實(shí)現(xiàn)方法
鏈接分享:http://chinadenli.net/article12/pghogc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、搜索引擎優(yōu)化、ChatGPT、面包屑導(dǎo)航、微信公眾號(hào)、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司