[关闭]
@hucheng91 2017-09-17T15:33:52.000000Z 字数 6933 阅读 654

angular 组件通信

angular angular2 angular4 component


angular组件通信是很长常见的功能,现在总结下,常见通信主要用一下三种

  1. 父组件 => 子组件
  2. 子组件 => 父组件
  3. 组件A = > 组件B
    Screenshot.png-19.9kB
父组件 => 子组件 子组件 => 父组件 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,这些同样基于函数式的状态管理几条街,下面一一说来

父组件 => 子组件

@input,最常用的一种方式

  1. @Component({
  2. selector: 'app-parent',
  3. template: '<div>childText:<app-child [textContent] = "varString"></app-child></div>',
  4. styleUrls: ['./parent.component.css']
  5. })
  6. export class ParentComponent implements OnInit {
  7. varString: string;
  8. constructor() { }
  9. ngOnInit() {
  10. this.varString = '从父组件传过来的' ;
  11. }
  12. }
  1. import { Component, OnInit, Input } from '@angular/core';
  2. @Component({
  3. selector: 'app-child',
  4. template: '<h1>{{textContent}}</h1>',
  5. styleUrls: ['./child.component.css']
  6. })
  7. export class ChildComponent implements OnInit {
  8. @Input() public textContent: string ;
  9. constructor() { }
  10. ngOnInit() {
  11. }
  12. }

setter

setter 是拦截@input 属性,因为我们在组件通信的时候,常常需要对输入的属性处理下,就需要setter了,setter和getter常配套使用,稍微修改下上面的child.component.ts
child.component.ts

  1. import { Component, OnInit, Input } from '@angular/core';
  2. @Component({
  3. selector: 'app-child',
  4. template: '<h1>{{textContent}}</h1>',
  5. styleUrls: ['./child.component.css']
  6. })
  7. export class ChildComponent implements OnInit {
  8. _textContent:string;
  9. @Input()
  10. set textContent(text: string){
  11. this._textContent = !text: "啥都没有给我" ? text ;
  12. } ;
  13. get textContent(){
  14. return this._textContent;
  15. }
  16. constructor() { }
  17. ngOnInit() {
  18. }
  19. }

onChange

这个是通过angular生命周期钩子来检测,不推荐使用,要使用的话可以参angular文档

@ViewChild()

@ViewChild() 一般用在调用子组件非私有的方法

  1. import {Component, OnInit, ViewChild} from '@angular/core';
  2. import {ViewChildChildComponent} from "../view-child-child/view-child-child.component";
  3. @Component({
  4. selector: 'app-parent',
  5. templateUrl: './parent.component.html',
  6. styleUrls: ['./parent.component.css']
  7. })
  8. export class ParentComponent implements OnInit {
  9. varString: string;
  10. @ViewChild(ViewChildChildComponent)
  11. viewChildChildComponent: ViewChildChildComponent;
  12. constructor() { }
  13. ngOnInit() {
  14. this.varString = '从父组件传过来的' ;
  15. }
  16. clickEvent(clickEvent: any) {
  17. console.log(clickEvent);
  18. this.viewChildChildComponent.myName(clickEvent.value);
  19. }
  20. }
  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'app-view-child-child',
  4. templateUrl: './view-child-child.component.html',
  5. styleUrls: ['./view-child-child.component.css']
  6. })
  7. export class ViewChildChildComponent implements OnInit {
  8. constructor() { }
  9. name: string;
  10. myName(name: string) {
  11. console.log(name);
  12. this.name = name ;
  13. }
  14. ngOnInit() {
  15. }
  16. }
  17. ```
  18. ### 局部变量
  19. 局部变量和viewChild类似,只能用在html模板里,修改parent.component.html,通过`#viewChild`这个变量来表示子组件,就能调用子组件的方法了.
  20. ```html
  21. <div class="panel-body">
  22. <input class="form-control" type="text" #viewChildInputName >
  23. <button class=" btn btn-primary" (click)="viewChild.myName(viewChildInputName.value)">局部变量传值</button>
  24. <app-view-child-child #viewChild></app-view-child-child>
  25. </div>
  26. <div class="md-section-divider"></div>

child 组件如下

  1. @Component({
  2. selector: 'app-view-child-child',
  3. templateUrl: './view-child-child.component.html',
  4. styleUrls: ['./view-child-child.component.css']
  5. })
  6. export class ViewChildChildComponent implements OnInit {
  7. constructor() { }
  8. name: string;
  9. myName(name: string) {
  10. console.log(name);
  11. this.name = name ;
  12. }
  13. ngOnInit() {
  14. }
  15. }
  16. <div class="md-section-divider"></div>

子组件 => 父组件

@output()

output这种常见的通信,本质是给子组件传入一个function,在子组件里执行完某些方法后,再执行传入的这个回调function,将值传给父组件

  1. parent.component.ts
  2. @Component({
  3. selector: 'app-child-to-parent',
  4. templateUrl: './parent.component.html',
  5. styleUrls: ['./parent.component.css']
  6. })
  7. export class ChildToParentComponent implements OnInit {
  8. childName: string;
  9. childNameForInject: string;
  10. constructor( ) { }
  11. ngOnInit() {
  12. }
  13. showChildName(name: string) {
  14. this.childName = name;
  15. }
  16. }
  17. <div class="md-section-divider"></div>

parent.component.html

  1. <div class="panel-body">
  2. <p>output方式 childText:{{childName}}</p>
  3. <br>
  4. <app-output-child (childNameEventEmitter)="showChildName($event)"></app-output-child>
  5. </div>
  6. <div class="md-section-divider"></div>
  1. child.component.ts
  2. export class OutputChildComponent implements OnInit {
  3. // 传入的回调事件
  4. @Output() public childNameEventEmitter: EventEmitter<any> = new EventEmitter();
  5. constructor() { }
  6. ngOnInit() {
  7. }
  8. showMyName(value) {
  9. //这里就执行,父组件传入的函数
  10. this.childNameEventEmitter.emit(value);
  11. }
  12. }

注入父组件

这个原理的原因是父,子组件本质生命周期是一样的

  1. export class OutputChildComponent implements OnInit {
  2. // 注入父组件
  3. constructor(private childToParentComponent: ChildToParentComponent) { }
  4. ngOnInit() {
  5. }
  6. showMyName(value) {
  7. this.childToParentComponent.childNameForInject = value;
  8. }
  9. }

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来演示

  1. user.service.ts
  2. @Injectable()
  3. export class UserService {
  4. age: number;
  5. userName: string;
  6. constructor() { }
  7. }
  8. app.module.ts
  9. @NgModule({
  10. declarations: [
  11. AppComponent,
  12. SiblingAComponent,
  13. SiblingBComponent
  14. ],
  15. imports: [
  16. BrowserModule
  17. ],
  18. providers: [UserService],
  19. bootstrap: [AppComponent]
  20. })
  21. export class AppModule { }
  22. SiblingBComponent.ts
  23. @Component({
  24. selector: 'app-sibling-b',
  25. templateUrl: './sibling-b.component.html',
  26. styleUrls: ['./sibling-b.component.css']
  27. })
  28. export class SiblingBComponent implements OnInit {
  29. constructor(private userService: UserService) {
  30. this.userService.userName = "王二";
  31. }
  32. ngOnInit() {
  33. }
  34. }
  35. SiblingAComponent.ts
  36. @Component({
  37. selector: 'app-sibling-a',
  38. templateUrl: './sibling-a.component.html',
  39. styleUrls: ['./sibling-a.component.css']
  40. })
  41. export class SiblingAComponent implements OnInit {
  42. userName: string;
  43. constructor(private userService: UserService) {
  44. }
  45. ngOnInit() {
  46. this.userName = this.userService.userName;
  47. }
  48. }

通过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

  1. import {Injectable} from "@angular/core";
  2. import {Subject} from "rxjs/Subject";
  3. @Injectable()
  4. export class AlertService {
  5. private messageSu = new Subject<string>(); //
  6. messageObserve = this.messageSu.asObservable();
  7. private setMessage(message: string) {
  8. this.messageSu.next(message);
  9. }
  10. public success(message: string, callback?: Function) {
  11. this.setMessage(message);
  12. callback();
  13. }
  14. }

sibling-a.component.ts

  1. @Component({
  2. selector: 'app-sibling-a',
  3. templateUrl: './sibling-a.component.html',
  4. styleUrls: ['./sibling-a.component.css']
  5. })
  6. export class SiblingAComponent implements OnInit {
  7. userName: string;
  8. constructor(private userService: UserService, private alertService: AlertService) {
  9. }
  10. ngOnInit() {
  11. this.userName = this.userService.userName;
  12. // 改变alertService的信息源
  13. this.alertService.success("初始化成功");
  14. }
  15. }

app.component.ts

  1. @Component({
  2. selector: 'app-root',
  3. templateUrl: './app.component.html',
  4. styleUrls: ['./app.component.css']
  5. })
  6. export class AppComponent {
  7. title = 'app';
  8. message: string;
  9. constructor(private alertService: AlertService) {
  10. //订阅alertServcie的message服务
  11. this.alertService.messageObserve.subscribe((res: any) => {
  12. this.message = res;
  13. });
  14. }
  15. }

这样订阅者就能动态的跟着发布源变化.

总结: 以上就是常用的的通信方式,各种场景可以采取不同的方法

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注