欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

angular中不同的組件間傳值與通信的方法

 更新時(shí)間:2017年11月04日 17:07:52   作者:xiaoming  
本篇文章主要介紹了angular中不同的組件間傳值與通信的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

本文主要介紹angular在不同的組件中如何進(jìn)行傳值,如何通訊。主要分為父子組件和非父子組件部分。

父子組件間參數(shù)與通訊方法

使用事件通信(EventEmitter,@Output):

場(chǎng)景:可以在父子組件之間進(jìn)行通信,一般使用在子組件傳遞消息給父組件;

步驟:

  1. 子組件創(chuàng)建事件EventEmitter對(duì)象,使用@output公開(kāi)出去;
  2. 父組件監(jiān)聽(tīng)子組件@output出來(lái)的方法,然后處理事件。

代碼:

 // child 組件
  @Component({
   selector: 'app-child',
   template: '',
   styles: [``]
  })
  export class AppChildComponent implements OnInit {
   @Output() onVoted: EventEmitter<any> = new EventEmitter();
   ngOnInit(): void {
    this.onVoted.emit(1);
   }
  }
  // parent 組件
  @Component({
   selector: 'app-parent',
   template: `
    <app-child (onVoted)="onListen($event)"></app-child>
   `,
   styles: [``]
  })
  export class AppParentComponent implements OnInit {
   ngOnInit(): void {
    throw new Error('Method not implemented.');
   }
   onListen(data: any): void {
    console.log('TAG' + '---------->>>' + data);
   }
  }

使用@ViewChild和@ViewChildren:

場(chǎng)景:一般用于父組件給子組件傳遞信息,或者父組件調(diào)用子組件的方法;

步驟:

  1. 父組件里面使用子組件;
  2. 父組件里面使用@ViewChild獲得子組件對(duì)象。
  3. 父組件使用子組件對(duì)象操控子組件;(傳遞信息或者調(diào)用方法)。

代碼:

// 子組件
@Component({
 selector: 'app-child',
 template: '',
 styles: [``]
})
export class AppChildComponent2 implements OnInit {
  data = 1;
  ngOnInit(): void {
 }
 getData(): void {
  console.log('TAG' + '---------->>>' + 111);
 }
}
// 父組件
@Component({
 selector: 'app-parent2',
 template: `
  <app-child></app-child>
 `,
 styles: [``]
})
export class AppParentComponent2 implements OnInit {
 @ViewChild(AppChildComponent2) child: AppChildComponent2;
 ngOnInit(): void {
  this.child.getData(); // 父組件獲得子組件方法
  console.log('TAG'+'---------->>>'+this.child.data);// 父組件獲得子組件屬性
 }
}

非父子組件參數(shù)傳遞與通訊方法

通過(guò)路由參數(shù)

場(chǎng)景:一個(gè)組件可以通過(guò)路由的方式跳轉(zhuǎn)到另一個(gè)組件 如:列表與編輯

步驟:

  1. A組件通過(guò)routerLink或router.navigate或router.navigateByUrl進(jìn)行頁(yè)面跳轉(zhuǎn)到B組件
  2. B組件接受這些參數(shù)

此方法只適用于參數(shù)傳遞,組件間的參數(shù)一旦接收就不會(huì)變化

代碼

傳遞方式

routerLink

<a routerLink=["/exampledetail",id]></a>

routerLink=["/exampledetail",{queryParams:object}]

routerLink=["/exampledetail",{queryParams:'id':'1','name':'yxman'}];

router.navigate

this.router.navigate(['/exampledetail',id]);
this.router.navigate(['/exampledetail'],{queryParams:{'name':'yxman'}});

router.navigateByUrl

this.router.navigateByUrl('/exampledetail/id');
this.router.navigateByUrl('/exampledetail',{queryParams:{'name':'yxman'}});

傳參方傳參之后,接收方2種接收方式如下:

snapshot

import { ActivateRoute } from '@angular/router';
public data: any;
export class ExampledetailComponent implements OnInit { 
  constructor( public route: ActivateRoute ) { };
  ngOnInit(){
    this.data = this.route.snapshot.params['id'];
  };
}

queryParams

import { ActivateRoute } from '@angular/router';
export class ExampledetailComponent implements OnInit { 
  public data: any;
  constructor( public activeRoute:ActivateRoute ) { };
  ngOnInit(){
    this.activeRoute.queryParams.subscribe(params => {
    this.data = params['name'];
  });
};

使用服務(wù)Service進(jìn)行通信,即:兩個(gè)組件同時(shí)注入某個(gè)服務(wù)

場(chǎng)景:需要通信的兩個(gè)組件不是父子組件或者不是相鄰組件;當(dāng)然,也可以是任意組件。

步驟:

  1. 新建一個(gè)服務(wù),組件A和組件B同時(shí)注入該服務(wù);
  2. 組件A從服務(wù)獲得數(shù)據(jù),或者想服務(wù)傳輸數(shù)據(jù)
  3. 組件B從服務(wù)獲得數(shù)據(jù),或者想服務(wù)傳輸數(shù)據(jù)。

代碼:

  // 組件A
  @Component({
   selector: 'app-a',
   template: '',
   styles: [``]
  })
  export class AppComponentA implements OnInit {
   constructor(private message: MessageService) {
   }
   ngOnInit(): void {
    // 組件A發(fā)送消息3
    this.message.sendMessage(3);
    const b = this.message.getMessage(); // 組件A接收消息;
   }
  }
  // 組件B
  @Component({
   selector: 'app-b',
   template: `
    <app-a></app-a>
   `,
   styles: [``]
  })
  export class AppComponentB implements OnInit {
   constructor(private message: MessageService) {
   }
   ngOnInit(): void {
    // 組件B獲得消息
    const a = this.message.getMessage();
    this.message.sendMessage(5); // 組件B發(fā)送消息
   }
  }

消息服務(wù)模塊

場(chǎng)景:這里涉及到一個(gè)項(xiàng)目,里面需要實(shí)現(xiàn)的是所有組件之間都有可能通信,或者是一個(gè)組件需要給幾個(gè)組件通信,且不可通過(guò)路由進(jìn)行傳參。

設(shè)計(jì)方式:

  1. 使用RxJs,定義一個(gè)服務(wù)模塊MessageService,所有的信息都注冊(cè)該服務(wù);
  2. 需要發(fā)消息的地方,調(diào)用該服務(wù)的方法;
  3. 需要接受信息的地方使用,調(diào)用接受信息的方法,獲得一個(gè)Subscription對(duì)象,然后監(jiān)聽(tīng)信息;
  4. 當(dāng)然,在每一個(gè)組件Destory的時(shí)候,需要
this.subscription.unsubscribe();

代碼:

  // 消息中專服務(wù)
  @Injectable()
  export class MessageService {
   private subject = new Subject<any>();
   /**
   * content模塊里面進(jìn)行信息傳輸,類似廣播
   * @param type 發(fā)送的信息類型
   *    1-你的信息
   *    2-你的信息
   *    3-你的信息
   *    4-你的信息
   *    5-你的信息
   */
   sendMessage(type: number) {
    console.log('TAG' + '---------->>>' + type);
    this.subject.next({type: type});
   }
   /**
   * 清理消息
   */
   clearMessage() {
    this.subject.next();
   }
   /**
   * 獲得消息
   * @returns {Observable<any>} 返回消息監(jiān)聽(tīng)
   */
   getMessage(): Observable<any> {
    return this.subject.asObservable();
   }
  }
  // 使用該服務(wù)的地方,需要注冊(cè)MessageService服務(wù);
  constructor(private message: MessageService) {
  }
  // 消息接受的地方;
  public subscription: Subscription;
  ngAfterViewInit(): void {
    this.subscription = this.message.getMessage().subscribe(msg => {
     // 根據(jù)msg,來(lái)處理你的業(yè)務(wù)邏輯。
    })
   }

   // 組件生命周期結(jié)束的時(shí)候,記得注銷一下,不然會(huì)卡;
   ngOnDestroy(): void {
    this.subscription.unsubscribe();
   }

   // 調(diào)用該服務(wù)的方法,發(fā)送信息;
   send():void {
    this.message.sendMessage(‘我發(fā)消息了,你們接受下'); // 發(fā)送信息消息
   }

這里的MessageService,就相當(dāng)于使用廣播機(jī)制,在所有的組件之間傳遞信息;不管是數(shù)字,字符串,還是對(duì)象都是可以傳遞的,而且這里的傳播速度也是很快的。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Angular2 http jsonp的實(shí)例詳解

    Angular2 http jsonp的實(shí)例詳解

    這篇文章主要介紹了Angular2 http jsonp的實(shí)例詳解的相關(guān)資料,希望通過(guò)本能幫助到大家,需要的朋友可以參考下
    2017-08-08
  • Angular.js中處理頁(yè)面閃爍的方法詳解

    Angular.js中處理頁(yè)面閃爍的方法詳解

    我們?cè)趹?yīng)用的頁(yè)面或者組件需要加載數(shù)據(jù)時(shí),瀏覽器和angular渲染頁(yè)面都需要消耗一定的時(shí)間。這里的間隔可能很小,甚至讓人感覺(jué)不到區(qū)別;但也可能很長(zhǎng),這樣會(huì)導(dǎo)致讓我們的用戶看到了沒(méi)有被渲染過(guò)的頁(yè)面。本文將介紹Angular.js中處理頁(yè)面閃爍的方法。
    2017-03-03
  • angular中不同的組件間傳值與通信的方法

    angular中不同的組件間傳值與通信的方法

    本篇文章主要介紹了angular中不同的組件間傳值與通信的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • Angular中使用響應(yīng)式表單的詳細(xì)步驟

    Angular中使用響應(yīng)式表單的詳細(xì)步驟

    Angular提供了兩種處理表單的方式模板驅(qū)動(dòng)表單和響應(yīng)式表單(也稱為模型驅(qū)動(dòng)表單),使用模板驅(qū)動(dòng)表單時(shí),模板指令被用來(lái)構(gòu)建表單的內(nèi)部表示,在本文中,您探討了如何將響應(yīng)式表單應(yīng)用于一個(gè)示例 Angular 應(yīng)用程序
    2024-02-02
  • 初學(xué)者AngularJS的環(huán)境搭建過(guò)程

    初學(xué)者AngularJS的環(huán)境搭建過(guò)程

    這篇文章主要介紹了初學(xué)者AngularJS的環(huán)境搭建過(guò)程,在文章給大家提到了Angular-cli的特性,大家一起看看吧
    2017-10-10
  • AngularJS 最常用的功能匯總

    AngularJS 最常用的功能匯總

    angularjs功能在項(xiàng)目開(kāi)發(fā)中經(jīng)常會(huì)用到,本文給大家總結(jié)了八種angularjs最常用的功能,感興趣的朋友一起學(xué)習(xí)吧
    2016-02-02
  • AngularJS控制器繼承自另一控制器

    AngularJS控制器繼承自另一控制器

    本文給大家介紹AngularJS控制器繼承自另一控制器的相關(guān)內(nèi)容,小編認(rèn)為介紹的非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友參考下吧
    2016-05-05
  • AngularJS實(shí)現(xiàn)單獨(dú)作用域內(nèi)的數(shù)據(jù)操作

    AngularJS實(shí)現(xiàn)單獨(dú)作用域內(nèi)的數(shù)據(jù)操作

    這篇文章給大家介紹了利用AngularJs如何實(shí)現(xiàn)ng-repeat內(nèi)各個(gè)小的子作用域單獨(dú)數(shù)據(jù)綁定。有需要的小伙伴們可以參考借鑒,下面來(lái)一起看看吧。
    2016-09-09
  • Angular.js實(shí)現(xiàn)注冊(cè)系統(tǒng)的實(shí)例詳解

    Angular.js實(shí)現(xiàn)注冊(cè)系統(tǒng)的實(shí)例詳解

    Angular.js是Google開(kāi)發(fā)的前端技術(shù)框架,最近一直在學(xué)習(xí)Angular.js,通過(guò)對(duì)angular.js的簡(jiǎn)單理解后發(fā)現(xiàn),angular.js通過(guò)一些簡(jiǎn)單的指令即可實(shí)現(xiàn)對(duì)DOM元素的操作,其特色為雙向數(shù)據(jù)綁定,下面這篇文章給大家詳細(xì)介紹Angular.js實(shí)現(xiàn)注冊(cè)系統(tǒng)的方法,一起來(lái)看看吧。
    2016-12-12
  • 淺談AngularJS中$http服務(wù)的簡(jiǎn)單用法

    淺談AngularJS中$http服務(wù)的簡(jiǎn)單用法

    這篇文章主要介紹了淺談AngularJS中$http服務(wù)的簡(jiǎn)單用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05

最新評(píng)論