angular6 组件间的交流方式 (2)-- viewChild 和 data service

我在上上篇文章中介绍了父子组件间经过 Input 和 Output 的交流方式。如今我会介绍组件间交流的其余两种方法,viewChild 和 data service。我将这篇文章分为两部分。首先看 viewChild 部分吧。css

经过 viewChild 传递数据

什么是 viewChild?

viewChild 就是准许一个组件被注入到别的组件中。而且指明了该可用子组件有了通往父组件的通道。简单说来,就是若是咱们想要子组件的特性,咱们就可使用 viewChild 这个神器。为了讲得详细点,如下面的代码为例。先建立两个组件,一个为父组件,另外一个为子组件。 下面是 child.component.tshtml

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
 counter :number = 0;
 IncreaseNumber():void{
  this.counter++;
}
  constructor() { }
  ngOnInit() {
  }
}

从这个代码块中,咱们能够知道,该组件有一个 counter 属性和一个 IncreaseNumber 方法。每次调用 IncreaseNumber 方法,counter的值就会自增一次。如今咱们看看父组件。 parent.component.tstypescript

import { Component, OnInit, ViewChild } from '@angular/core';
import{ChildComponent} from './child.component'
@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit {
  @ViewChild(ChildComponent) private childcomponent : ChildComponent;
Increase():void{
  this.childcomponent.IncreaseNumber();
}
Dicrease():void{
  this.childcomponent.counter--;
}
constructor() { }
  ngOnInit() {
  }
}

如今一步步分析上面的代码块。app

  • import {ViewChild} from '@angular/core',再 import进子组件。

@ViewChild(ChildComponent) private childcomponent : ChildComponent;ide

这句代码很是重要,用于查询子组件,并在本地建立的子组件对象 childcomponent 中注入相同的属性。父组件一样有两个方法,自增和自减。父组件的模板能够改为这样子:this

<p>
   <b>@ViewChild with Components..</b>
<br>
<br>
<input type="button" (click)="Increase()">
<input type="button" (click)="Decrease()">
<app-child></app-child>
</p>

能够看到,咱们在父组件模板中实例化了子组件。code

经过 data service 传递数据

到目前为止,咱们已经知道了如何在相关组件间传递数据,那么若是是不想关的组件呢?咱们一般会用 service 来实现数据传递。共享数据背后的思想是共享服务,他将全部被组件中用到的数据同步在一块儿。就像下面这个例子: 首先来个data service:Data.service.tscomponent

import { Injectable } from '@angular/core';
import {BehaviorSubject} from 'rxjs'
@Injectable()
export class DataService {
    private messageSource = new BehaviorSubject < string > ("Start");
    constructor() {}
    changemessage(message: string): void {
        this.messageSource.next(message);
    }
}

如今咱们来分析一下上面这个代码块:htm

  • import { Injectable } from '@angular/core' 引进 @Injectable 装饰器,这可让其余组件和模块使用该服务类的功能。
  • import {BehaviorSubject} from 'rxjs' 这能够返回一种Observable类型,从而订阅不一样类型的消息。这个 service 里面定义了一种changemessage(),它会返回从 Observable 那里获取到的数据。为了让各组件可使用到这个服务,咱们须要在根模块中将这个 service 以 provider:[DataService]的形式引入。 引入该服务的组件 service.component.ts
import { Component, OnInit } from '@angular/core';
import{DataService} from '../data.service';
@Component({
  selector: 'app-secondcomponent',
  templateUrl: './secondcomponent.component.html',
  styleUrls: ['./secondcomponent.component.css']
})
export class SecondcomponentComponent implements OnInit {
    childmessage: string = "";
    counter:number = 0;
    constructor(private data: DataService) {}
    ngOnInit() {
        this.data.currentmessage.subscribe(Message => this.childMessage);
    }
    newmessage(): void {
        this.data.changemessage("changing counter from sibling " + this.counter++);
    }
}

总结:对象

  1. 经过 @Injectable 装饰器添加 service;
  2. 使用 RxJs 中的 BehaviorSubject,返回数据为 Observable 类型;
  3. 订阅一些可变属性,在组件中给它从新赋值。

参考:https://dzone.com/articles/angular-component-communication-day-2

相关文章
相关标签/搜索