Angular 组件通讯方式
(一)父子组件 输入/输出属性
关键词 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 组件通讯方式的更多相关文章
- vue 组件通讯方式到底有多少种 ?
前置 做大小 vue 项目都离不开组件通讯, 自己也收藏了很多关于 vue 组件通讯的文章. 今天自己全部试了试, 并查了文档, 在这里总结一下并全部列出, 都是简单的例子. 如有错误欢迎指正. 温馨 ...
- Angular1.x组件通讯方式总结
Angular1开发模式 这里需要将Angular1分为Angular1.5之前和Angular1.5两个不同的阶段来讲,两者虽然同属Angular1,但是在开发模式上还是有较大区别的.在Angula ...
- Angular1组件通讯方式总结
这里需要将Angular1分为Angular1.5之前和Angular1.5两个不同的阶段来讲,两者虽然同属Angular1,但是在开发模式上还是有较大区别的.在Angular1.4及以前,主要是基于 ...
- angular,vue,react的基本语法—动态属性、事件绑定、ref,angular组件创建方式
基本语法: 动态属性: vue: v-bind:attr="msg" :attr="msg" react: attr={msg} angular [attr]= ...
- Angular 组件通讯、生命周期钩子 小结
本文为原创,转载请注明出处: cnzt 文章:cnzt-p http://www.cnblogs.com/zt-blog/p/7986858.html http://www.cnblogs ...
- vue组件通讯之provide / inject
什么是 provide / inject [传送门] vue的组件通讯方式我们熟知的有 props $emit bus vuex ,另外就是 provide/inject provide/inject ...
- Angular组件——父子组件通讯
Angular组件间通讯 组件树,1号是根组件AppComponent. 组件之间松耦合,组件之间知道的越少越好. 组件4里面点击按钮,触发组件5的初始化逻辑. 传统做法:在按钮4的点击事件里调用组件 ...
- angular组件之间的通讯
组件通讯,意在不同的指令和组件之间共享信息.如何在两个多个组件之间共享信息呢. 最近在项目上,组件跟组件之间可能是父子关系,兄弟关系,爷孙关系都有.....我也找找了很多关于组件之间通讯的方法,不同的 ...
- Angular组件之间通讯
组件之间会有下列3种关系: 1. 父子关系 2. 兄弟关系 3. 没有直接关系 通常采用下列方式处理(某些方式是框架特有)组件间的通讯,如下: 1父子组件之间的交互(@Input/@Output/模板 ...
随机推荐
- H5 FileReader对象
前言:FileReader是一种异步文件读取机制,结合input:file可以很方便的读取本地文件. input:file 在介绍FileReader之前,先简单介绍input的file类型. < ...
- 线程协作之threading.Condition
领会下面这个示例吧,其实跟java中wait/nofity是一样一样的道理 import threading # 条件变量,用于复杂的线程间同步锁 """ 需求: 男:小 ...
- echart-如何将x轴和y轴的原点进行重合???
设计稿突然让x轴 和y轴重合,我们可以设置图中的这个属性. 不知道还有没有别的设置属性,欢迎评论指出谢谢
- 01-pandas基础-Series与DataFrame
一.Series: 1,介绍:Series是以中类似于一维数组的对象,由一维数组以及与之相关的标签组成 特点:索引在左边,值在右边.在创建时,若我们未给数据指定索引,Series会自动创建一个0到N- ...
- Web开发者易犯的五大严重错误
无论你是编程高手,还是技术爱好者,在进行Web开发过程中,总避免不了犯各种各样的错误. 犯了错误,可以改正.但如果犯了某些错误,则会带来重大损失.遗憾.令人惊讶的是,这些错误往往是最普通,最容易避免. ...
- 大数据笔记(十七)——Pig的安装及环境配置、数据模型
一.Pig简介和Pig的安装配置 1.最早是由Yahoo开发,后来给了Apache 2.支持语言:PigLatin 类似SQL 3.翻译器 PigLatin ---> MapReduce(Spa ...
- np.asarray(a, dtype=None, order=None)
np.asarray(a, dtype=None, order=None) 参数a:可以是,列表, 列表的元组, 元组, 元组的元组, 元组的列表,多维数组 参数dtype=None, order=N ...
- Qt之zip压缩/解压缩(QuaZIP)
摘要: 简述 QuaZIP是使用Qt/C++对ZLIB进行简单封装的用于压缩及解压缩ZIP的开源库.适用于多种平台,利用它可以很方便的将单个或多个文件打包为zip文件,且打包后的zip文件可以通过其它 ...
- 个性化对待亚马逊不同站点 使用 Python 进行线程编程
# -*- coding: UTF-8 -*- import threading import time exitFlag = 0 class myThread (threading.Thread): ...
- 使用spring提供的@Scheduled注解创建定时任务
使用方法 操作非常简单,只要按如下几个步骤配置即可 1. 导入jar包或添加依赖,其实定时任务只需要spring-context即可,当然起服务还需要spring-web: 2. 编写定时任务类和方法 ...