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/模板 ...
随机推荐
- RouterOS基础安装配置
安装 光盘安装 载入光盘,按"A"全选按"I"进行安装,然后提示不否用旧的配置,按"N"全新安装,警告时按"Y"确定 安 ...
- Python---面向对象---案例
a = 5print(5/10)# 地板除(功能类似于数学模块当中floor()向下取整操作)print(5//10)print(a%10)b = 25print(b/10)print(b//10)p ...
- NOIP模拟赛(by hzwer) T3 小奇回地球
[题目背景] 开学了,小奇在回地球的路上,遇到了一个棘手的问题. [问题描述] 简单来说,它要从标号为 1 的星球到标号为 n 的星球,某一些星球之间有航线. 由于超时空隧道的存在,从一个星球到另一个 ...
- 【微信小程序】基础组件--view text image
组件的通用属性: id class style hidden bind* catch* data-* view 小程序基础组件,基本等于最常用组件,类似于HTML中的div.view用于构建页面骨架, ...
- php array_pop()函数 语法
php array_pop()函数 语法 作用:删除数组中的最后一个元素.博智达 语法:array_pop(array) 参数: 参数 描述 array 必需.规定数组. 说明:返回数组的最后 ...
- JavaScript性能优化之小知识总结
1.避免全局查找 在一个函数中会用到全局对象存储为局部变量来减少全局查找,因为访问局部变量的速度要比访问全局变量的速度更快些 2.定时器 如果针对的是不断运行的代码,不应该使用setTimeout,而 ...
- HDU-6703 array
Description You are given an array a1,a2,...,an(∀i∈[1,n],1≤ai≤n). Initially, each element of the arr ...
- angular 发送ajax
在使用angular发送ajax的时候get和post一样的,就是method改一下. ajax的js: <script> var app = angular.module('emialV ...
- 认识了一个新的手机游戏剖析工具- SnapDragon Profiler
原来这个是高通的工具,具说UNITY官方推荐了这个工具.大概看了下,可以从宏观上实时剖析手机应用的方方面面
- Java中的BigDecimal类和int和Integer总结
前言 我们都知道浮点型变量在进行计算的时候会出现丢失精度的问题.如下一段代码: System.out.println(0.05 + 0.01); System.out.println(1.0 - 0. ...