(一)父子组件 输入/输出属性

     关键词  Input,Output,EventEmitter。

   父子组件信息信息,分为

    (1)子组件向父组件传递

    (2)父组件向子组件传递

(二)模版变量与 @ViewChild

     (1)模版变量

     给子组件设定一个Id值,在HTML页面中,直接通过Id值,操作子组件的属性值。

import {Component, OnInit} from '@angular/core';
@Component({
selector: 'app-child-component',
template: `I'm {{ name }}`
})
export class ChildComponent implements OnInit {
public name: string;
constructor() {}
ngOnInit() {}
}

  父组件

import {Component, OnInit} from '@angular/core';
@Component({
selector: 'app-parent-component',
template: `
<app-child-component #child></app-child-component>
<br>
<button (click)="child.name = childName">设置子组件名称</button>
`
}) export class ParentComponent implements OnInit {
private childName: string;
constructor() { }
ngOnInit() {
this.childName = 'jack child';
}
}

  (2) @ViewChild

     与模版变量类似,在ts代码里操作子组件的属性值。

    子组件

import {Component, OnInit} from '@angular/core';

@Component({
selector: 'app-child-component',
template: `I'm {{ name }}`
})
export class ChildComponent implements OnInit {
public name: string;
constructor() { }
ngOnInit() { }
}

    父组件

import {Component, OnInit, ViewChild} from '@angular/core';
import { ChildComponent} from './t11';
@Component({
selector: 'app-parent-component',
template: `
<app-child-component #child></app-child-component>
<br>
<button (click)="setChildName()">设置子组件名称</button>
`
}) export class ParentComponent implements OnInit {
@ViewChild(ChildComponent)
childCmp: ChildComponent;
constructor() { }
ngOnInit() { }
setChildName() {
this.childCmp.name = 'give it jack child';
}
}

(三)RxJS Subject

   使用关键词  Observable,Subject,Subscription。

   这种方式通过一个 Service 实例作为中央事件总线,用它来触发事件和监听事件,从而实现任何组件间的通信,包括 父子,兄弟,跨级。

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class MessageService {
subject = new Subject<any>();
getMessage() {
return this.subject.asObservable();
} sendMessage(msg){
this.subject.next(msg);
} clearMessage() {
this.subject.next();
}
}

子组件

import { Component, OnInit } from '@angular/core';
import { MessageService } from '../service/message.service';
@Component({
selector: 'app-child',
template: `
<div>
<button (click)="sendMessage()">Send Message</button>
<button (click)="clearMessage()">Clear Message</button>
</div>`
})
export class ChildComponent implements OnInit {
constructor(private messageService: MessageService) {
} ngOnInit() {
}
sendMessage(): void {
this.messageService.sendMessage('hello from child ');
} clearMessage(): void {
this.messageService.clearMessage();
}
}

父组件

import { Component, OnInit } from '@angular/core';
import { MessageService } from '../service/message.service';
import { Subscription } from 'rxjs'; @Component({
selector: 'app-parent',
template: `
<div>
<div *ngIf="message">{{message}}</div>
<app-child></app-child>
</div>
`,
providers: [ MessageService]
}) export class ParentTestComponent implements OnInit {
message;
subscription = new Subscription();
constructor(private messageService: MessageService) {
this.subscription = this.messageService.getMessage().subscribe( res => {
this.message = res;
});
}
ngOnInit() {
this.subscription.unsubscribe();
}
}

 总结:

   从继承关系来看,

    1、 class EventEmitter<T> extends Subject<T> 。

    2、class Subject<T> extends Observable<T> implements SubscriptionLike

    Subscription implements SubscriptionLike

    4、interface SubscriptionLike extends Unsubscribable

   

Angular 组件通讯方式的更多相关文章

  1. vue 组件通讯方式到底有多少种 ?

    前置 做大小 vue 项目都离不开组件通讯, 自己也收藏了很多关于 vue 组件通讯的文章. 今天自己全部试了试, 并查了文档, 在这里总结一下并全部列出, 都是简单的例子. 如有错误欢迎指正. 温馨 ...

  2. Angular1.x组件通讯方式总结

    Angular1开发模式 这里需要将Angular1分为Angular1.5之前和Angular1.5两个不同的阶段来讲,两者虽然同属Angular1,但是在开发模式上还是有较大区别的.在Angula ...

  3. Angular1组件通讯方式总结

    这里需要将Angular1分为Angular1.5之前和Angular1.5两个不同的阶段来讲,两者虽然同属Angular1,但是在开发模式上还是有较大区别的.在Angular1.4及以前,主要是基于 ...

  4. angular,vue,react的基本语法—动态属性、事件绑定、ref,angular组件创建方式

    基本语法: 动态属性: vue: v-bind:attr="msg" :attr="msg" react: attr={msg} angular [attr]= ...

  5. Angular 组件通讯、生命周期钩子 小结

    本文为原创,转载请注明出处: cnzt       文章:cnzt-p http://www.cnblogs.com/zt-blog/p/7986858.html http://www.cnblogs ...

  6. vue组件通讯之provide / inject

    什么是 provide / inject [传送门] vue的组件通讯方式我们熟知的有 props $emit bus vuex ,另外就是 provide/inject provide/inject ...

  7. Angular组件——父子组件通讯

    Angular组件间通讯 组件树,1号是根组件AppComponent. 组件之间松耦合,组件之间知道的越少越好. 组件4里面点击按钮,触发组件5的初始化逻辑. 传统做法:在按钮4的点击事件里调用组件 ...

  8. angular组件之间的通讯

    组件通讯,意在不同的指令和组件之间共享信息.如何在两个多个组件之间共享信息呢. 最近在项目上,组件跟组件之间可能是父子关系,兄弟关系,爷孙关系都有.....我也找找了很多关于组件之间通讯的方法,不同的 ...

  9. Angular组件之间通讯

    组件之间会有下列3种关系: 1. 父子关系 2. 兄弟关系 3. 没有直接关系 通常采用下列方式处理(某些方式是框架特有)组件间的通讯,如下: 1父子组件之间的交互(@Input/@Output/模板 ...

随机推荐

  1. Android PdfViewer案例使用

    今天按项目要求找了一个android的PDF控件,各种操作效果都非常好,在这里和大家分享一下. com.joanzapata.pdfview:android-pdfview  该PDF控件加载大存储的 ...

  2. 【牛客Wannafly挑战赛23】F 计数

    题目链接 题意 给定一张边带权的无向图,求生成树的权值和是 k 的倍数的生成树个数模 p 的值. \(n\leq 100,k\leq 100,p\mod k=1\) Sol 看见整除然后 \(p\mo ...

  3. python-获取类名和方法名,动态创建类和方法及属性

    获取类名和方法名1.在函数外部获取函数名称,用.__name__获取2.在函数内部获取当前函数名称,用sys._getframe().f_code.co_name方法获取3.使用inspect模块动态 ...

  4. 阿里云产品家族再添新丁:视觉AI、CPFS一体机助力企业全面上云

    近日举行的2019阿里云广东峰会上,阿里云宣布推出面向混合云场景的CPFS一体机和视觉AI一体机,两款新品具备超高性能.开箱即用等特性,极大降低企业上云的周期和门槛. 加上此前推出的POLARDB数据 ...

  5. 8,HashMap子类-LinkedHashMap

    在上一篇随笔中,分析了HashMap的源码,里面涉及到了3个钩子函数(afterNodeAccess(e),afterNodeInsertion(evict),afterNodeRemoval(nod ...

  6. 搭建nginx环境(参考腾讯云实验室)

    使用 yum 安装 Nginx: yum install nginx -y 修改 /etc/nginx/conf.d/default.conf,去除对 IPv6 地址的监听,可参考下面的代码示例: s ...

  7. Activiti7整合SpringBoot(十二)

    1 SpringBoot 整合 Activiti7 的配置 为了能够实现 SpringBoot 与 Activiti7 整合开发,首先我们要引入相关的依赖支持.所以,我们在工程的 pom.xml 文件 ...

  8. 关于VMware 15搭建MacOS 10.14后无法播放在线视频和客户端视频的问题

    最近在自己的电脑上搭建了MacOS10.14系统,搭建是成功了,但是发现一个很坑的事,看视频发现黑屏.就是那种只有声音,没有视频的问题,在多个浏览器上和客户端都是一样的.百度了下,总结有2种可能,一是 ...

  9. Vim 编辑器学习笔记

    参考资料: 世界上最牛的编辑器: Vim 1

  10. bootstrap插件bootstrap-select使用demo

    插件bootstrap-select官网 : https://developer.snapappointments.com/bootstrap-select/ bootstrap-select插件: ...