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/模板 ...
随机推荐
- 【leetcode】1124. Longest Well-Performing Interval
题目如下: We are given hours, a list of the number of hours worked per day for a given employee. A day i ...
- echart-折线图,数据太多想变成鼠标拖动和滚动的效果?以及数据的默认圈圈如何自定义圆圈的样式
1.数据太多怎么办???想拖拽,想滑动 dataZoom: [ { type: 'slider', } ] dataZoom: [ { type: 'inside', }] 两种功能都需要,还想调样 ...
- Android开发实践:Android.mk模板
关于Android NDK开发的文章已经比较多了,我的博客中也分享了很多NDK开发相关经验和技巧,今天简单写了一个 Android.mk 的示例模板,供初学者参考. 本模板主要给大家示例 Androi ...
- C#删除文件夹的文件
using System.IO; //判断文件是不是存在if(File.Exists(@"文件路径")){//如果存在则删除File.Delete(@"文件路径" ...
- Apache主要配置文件http.conf
1.基本概念 Define SRVROOT "/Apache24" ServerRoot "${SRVROOT}" #Apache安装的根路径 #Listen ...
- luogu【P1024 一元三次方程求解】题解
题目描述 有形如:ax3+bx2+cx+d=0 这样的一个一元三次方程.给出该方程中各项的系数(a,b,c,d 均为实数),并约定该方程存在三个不同实根(根的范围在-100至100之间),且根与根之差 ...
- js返回上一页并刷新的几种方法
1.返回上一页 1)<a href="javascript:history.go(-1)"></a> 2)<a href="javascri ...
- VUE环境搭建,项目配置(Windows下)
公司想做官网,框架我自己定,然后就选了vue,那现在就来加深一遍vue的环境的搭建吧 1.安装node.js,这里就不再多说了,很简单,如果之前有安装就不用再安装了,可node -v查看node版本 ...
- D - Cheerleaders(第三周)
D - Cheerleaders 题目链接:https://vjudge.net/contest/154063#problem/D 题目大意: 给你一个 n∗m 的方格,现在有 k 个相同石子,我们要 ...
- TYPORA的使用手册
Typora可以根据当前文档的标题层级,自动生成并显示大纲,窗口的右下角并有字数显示. 1.标题的使用标题的使用格式# 一阶标题 或者快捷键Ctrl+1 ##二阶标题 或者快捷键Ctrl+2 ###三 ...