angular 組件通訊

單頁面應用組件通訊有如下幾種,這篇文章主要講 Angular 通訊javascript

Screenshot.png-19.9kB

  1. 父組件 => 子組件
  2. 子組件 => 父組件
  3. 組件A = > 組件B
父組件 => 子組件 子組件 => 父組件 sibling => sibling
@input @output
setters (本質上仍是@input) 注入父組件
ngOnChanges() (不推薦使用)
局部變量
@ViewChild()
service service service
Rxjs的Observalbe Rxjs的Observalbe Rxjs的Observalbe
localStorage,sessionStorage localStorage,sessionStorage localStorage,sessionStorage

上面圖表總結了能用到通訊方案,期中最後3種,是通用的,angular的組件之間均可以使用這3種,其中Rxjs是最最牛逼的用法,甩redux,promise,這些一樣基於函數式的狀態管理幾條街,下面一一說來css

父組件 => 子組件

@input,最經常使用的一種方式

@Component({
  selector: 'app-parent',
template: '<div>childText:<app-child  [textContent] = "varString"></app-child></div>',
  styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit {
  varString: string;
  constructor() { }
  ngOnInit() {
    this.varString = '從父組件傳過來的' ;
  }
}
import { Component, OnInit, Input } from '@angular/core';
@Component({
  selector: 'app-child',
  template: '<h1>{{textContent}}</h1>',
  styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
  @Input() public textContent: string ;
  constructor() { }
  ngOnInit() {
  }
}

setter

setter 是攔截@input 屬性,由於咱們在組件通訊的時候,經常須要對輸入的屬性處理下,就須要setter了,setter和getter常配套使用,稍微修改下上面的child.component.ts
child.component.tshtml

import { Component, OnInit, Input } from '@angular/core';
@Component({
  selector: 'app-child',
  template: '<h1>{{textContent}}</h1>',
  styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
_textContent:string;
  @Input()
  set textContent(text: string){
   this._textContent = !text: "啥都沒有給我" ? text ;
  } ;
  get textContent(){
  return this._textContent;
  }
  constructor() { }
  ngOnInit() {
  }
}

onChange

這個是經過angular生命週期鉤子來檢測,不推薦使用,要使用的話能夠參angular文檔前端

@ViewChild()

@ViewChild() 通常用在調用子組件非私有的方法java

import {Component, OnInit, ViewChild} from '@angular/core';
       import {ViewChildChildComponent} from "../view-child-child/view-child-child.component";
    @Component({
      selector: 'app-parent',
      templateUrl: './parent.component.html',
      styleUrls: ['./parent.component.css']
    })
    export class ParentComponent implements OnInit {
      varString: string;
      @ViewChild(ViewChildChildComponent)
      viewChildChildComponent: ViewChildChildComponent;
      constructor() { }
      ngOnInit() {
        this.varString = '從父組件傳過來的' ;
      }
      clickEvent(clickEvent: any) {
        console.log(clickEvent);
        this.viewChildChildComponent.myName(clickEvent.value);
      }
    }
import { Component, OnInit } from '@angular/core';
    @Component({
      selector: 'app-view-child-child',
      templateUrl: './view-child-child.component.html',
      styleUrls: ['./view-child-child.component.css']
    })
    export class ViewChildChildComponent implements OnInit {
      constructor() { }
      name: string;
      myName(name: string) {
          console.log(name);
          this.name = name ;
      }
      ngOnInit() {
      }
    }

局部變量

局部變量和viewChild相似,只能用在html模板裏,修改parent.component.html,經過#viewChild這個變量來表示子組件,就能調用子組件的方法了.ajax

<div class="panel-body">
    <input class="form-control" type="text" #viewChildInputName >
    <button class=" btn btn-primary" (click)="viewChild.myName(viewChildInputName.value)">局部變量傳值</button>
    <app-view-child-child #viewChild></app-view-child-child>
            </div>

child 組件以下redux

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

  constructor() { }
  name: string;
  myName(name: string) {
      console.log(name);
      this.name = name ;
  }
  ngOnInit() {
  }

}

子組件 => 父組件

@output()

output這種常見的通訊,本質是給子組件傳入一個function,在子組件裏執行完某些方法後,再執行傳入的這個回調function,將值傳給父組件bootstrap

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

  childName: string;
  childNameForInject: string;
  constructor( ) { }
  ngOnInit() {
  }
  showChildName(name: string) {
    this.childName = name;
  }
}

parent.component.htmlpromise

<div class="panel-body">
  <p>output方式 childText:{{childName}}</p>
  <br>
  <app-output-child (childNameEventEmitter)="showChildName($event)"></app-output-child>
</div>
child.component.ts
  export class OutputChildComponent implements OnInit {
  // 傳入的回調事件
  @Output() public childNameEventEmitter: EventEmitter<any> = new EventEmitter();
  constructor() { }
  ngOnInit() {
  }
  showMyName(value) {
    //這裏就執行,父組件傳入的函數
    this.childNameEventEmitter.emit(value);
  }
}

注入父組件

這個原理的緣由是父,子組件本質生命週期是同樣的session

export class OutputChildComponent implements OnInit {
  // 注入父組件
  constructor(private childToParentComponent: ChildToParentComponent) { }
  ngOnInit() {
  }
  showMyName(value) {
    this.childToParentComponent.childNameForInject = value;
  }
}

sibling組件 => sibling組件

service

Rxjs

經過service通訊

angular中service是單例的,因此三種通訊類型均可以經過service,不少前端對單例理解的不是很清楚,本質就是
,你在某個module中注入service,全部這個modul的component均可以拿到這個service的屬性,方法,是共享的,因此常在app.moudule.ts注入日誌service,http攔截service,在子module注入的service,只能這個子module能共享,在component注入的service,就只能子的component的能拿到service,下面以注入到app.module.ts,的service來演示

user.service.ts
@Injectable()
export class UserService {
  age: number;
  userName: string;
  constructor() { }
}
app.module.ts
@NgModule({
  declarations: [
    AppComponent,
    SiblingAComponent,
    SiblingBComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [UserService],
  bootstrap: [AppComponent]
})
export class AppModule { }
SiblingBComponent.ts
@Component({
  selector: 'app-sibling-b',
  templateUrl: './sibling-b.component.html',
  styleUrls: ['./sibling-b.component.css']
})
export class SiblingBComponent implements OnInit {
  constructor(private userService: UserService) {
    this.userService.userName = "王二";
  }
  ngOnInit() {
  }
}
SiblingAComponent.ts
@Component({
  selector: 'app-sibling-a',
  templateUrl: './sibling-a.component.html',
  styleUrls: ['./sibling-a.component.css']
})
export class SiblingAComponent implements OnInit {
  userName: string;
  constructor(private userService: UserService) {
  }
  ngOnInit() {
    this.userName = this.userService.userName;
  }
}

經過Rx.js通訊

這個是最牛逼的,基於訂閱發佈的這種流文件處理,一旦訂閱,發佈的源頭髮生改變,訂閱者就能拿到這個變化;這樣說不是很好理解,簡單解釋就是,b.js,c.js,d.js訂閱了a.js裏某個值變化,b.js,c.js,d.js立馬獲取到這個變化的,可是a.js並無主動調用b.js,c.js,d.js這些裏面的方法,舉個簡單的例子,每一個頁面在處理ajax請求的時候,都有一彈出的提示信息,通常我會在
組件的template中中放一個提示框的組件,這樣很繁瑣每一個組件都要來一次,若是基於Rx.js,就能夠在app.component.ts中放這個提示組件,而後app.component.ts訂閱公共的service,就比較省事了,代碼以下
首先搞一個alset.service.ts

import {Injectable} from "@angular/core";
import {Subject} from "rxjs/Subject";
@Injectable()
export class AlertService {
  private messageSu = new Subject<string>();  //
  messageObserve = this.messageSu.asObservable();
  private  setMessage(message: string) {
    this.messageSu.next(message);
  }
  public success(message: string, callback?: Function) {
    this.setMessage(message);
    callback();
  }
}

sibling-a.component.ts

@Component({
  selector: 'app-sibling-a',
  templateUrl: './sibling-a.component.html',
  styleUrls: ['./sibling-a.component.css']
})
export class SiblingAComponent implements OnInit {
  userName: string;
  constructor(private userService: UserService, private alertService: AlertService) {
  }
  ngOnInit() {
    this.userName = this.userService.userName;
    // 改變alertService的信息源
    this.alertService.success("初始化成功");
  }
}

app.component.ts

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  message: string;
  constructor(private alertService: AlertService) {
    //訂閱alertServcie的message服務
     this.alertService.messageObserve.subscribe((res: any) => {
      this.message = res;
    });
  }
}

這樣訂閱者就能動態的跟着發佈源變化

總結: 以上就是經常使用的的通訊方式,各類場景能夠採起不一樣的方法

個人博客

相關文章
相關標籤/搜索