angular2+ 组件间通信
angular2+ 不同于react的redux,vue的vuex,angular2+其实可实现数据状态管理的方法很多,以下方案一般也足够支撑普通业务;
父子组件通信
1.1 父组件向子组件传递信息(@Input)
- 父组件绑定信息
<app-child childTitle="可设置子组件标题"></app-child>
- 子组件接收消息
import { Component, OnInit, Input } from '@angular/core';
@Input childTitle: string;
1.2 子组件向父组件传递信息
- 子组件使用 EventEmitter 传递消息
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
@Output() initEmit = new EventEmitter<string>();
ngOnInit() {
this.initEmit.emit("子组件初始化成功");
}
- 父组件接收消息
<app-child (initEmit)="accept($event)"></app-child>
accept(msg:string) {
alert(msg);
}
使用 ViewChild
// 父组件
import { Component, ViewChild, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
Message: {{message}}
<app-child></app-child>
`,
styleUrls: ['./parent.component.css']
})
export class ParentComponent implements AfterViewInit {
@ViewChild(ChildComponent) child;
constructor() {}
message: string;
ngAfterViewInit() {
this.message = this.child.message;
}
}
// 子组件
import { Component } from '@angular/core';
@Component({
message: string = 'Hola Mundo';
constructor() {}
})
非父子组件通信
- service服务
// service.ts
import { Component, Injectable, EventEmitter } from "@angular/core";
@Injectable()
export class myService {
public info: string = "";
constructor() {}
}
组件 1 向 service 传递信息
import { myService } from '../../service/myService.service';
...
constructor(
public service: myService
) { }
changeInfo() {
this.service.info = this.service.info + "1234";
}
组件 2 从 service 获取信息
import { myService } from '../../service/myService.service';
constructor(
public service: myService
) { }
showInfo() {
alert(this.service.info);
}
- 使用 BehaviorSubject
优点:真正的发布订阅模式,当数据改变时,订阅者也能得到响应
// service
import { BehaviorSubject } from 'rxjs';
public messageSource = new BehaviorSubject<string>('Start');
changemessage(message: string): void {
this.messageSource.next(message);
}
组件调用 service 的方法传信息和接收信息
changeInfo() {
this.communication.changemessage('Message from child 1.');
}
ngOnInit() {
this.communication.messageSource.subscribe(Message => {
window.alert(Message);
this.info = Message;
});
}
rxjs的observable
- 父组件:app.component.ts、app.component.html
- 子组件:home.component.html、home.component.html
- 服务:shared.service.ts
关键方法 - Observable.subscribe() 用于订阅发送可观察对象的消息
- Subject.next() 用于向观察者对象发送消息,然后将其发送给改观察者的所有订阅者
- Subject.asObservable() 返回一个可观察对象,一旦值变化,便会同时向它的订阅者更新消息。
shared.service公共服务
//shared.service.ts
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class SharedService {
private msg = new Subject<any>();
//发送消息
sendMessage(message: string) {
this.msg.next(message);
}
//清除消息
clearMessage() {
this.msg.next();
}
//获取消息
getMessage(): Observable<any> {
return this.msg.asObservable();
}
}
app父组件获取消息
<!--app.component.html-->
<p-growl [(value)]="alertMsg"></p-growl>
//app.component.ts
public alertMsg: Array<object>;
constructor(private sharedService: SharedService) {}
ngOnInit() {
//消息提示 从service获取消息内容
this.sharedService.getMessage().subscribe(value => {
this.alertMsg = value;
})
}
home子组件发送消息
<!--home.component.html-->
<button (click)="sendMessage()">Send Message</button>
//home.component.ts
constructor(private sharedService: SharedService) {}
public sendMessage() {
//发送消息
this.sharedService.sendMessage('显示成功');
}
其他的通信方式
- 路由传值
- cookie、session、storage
angular2+ 组件间通信的更多相关文章
- React独立组件间通信联动
React是现在主流的高效的前端框架,其官方文档 http://reactjs.cn/react/docs/getting-started.html 在介绍组件间通信时只给出了父子组件间通信的方法,而 ...
- 聊聊Vue.js组件间通信的几种姿势
写在前面 因为对Vue.js很感兴趣,而且平时工作的技术栈也是Vue.js,这几个月花了些时间研究学习了一下Vue.js源码,并做了总结与输出. 文章的原地址:https://github.com/a ...
- 【Vue】利用父子组件间通信实现一个场景
组件间通信是组件开发的,我们既希望组件的独立性,数据能互不干扰,又不可避免组件间会有联系和交互. 在vue中,父子组件的关系可以总结为props down,events up: 在vue2.0中废弃了 ...
- React 精要面试题讲解(二) 组件间通信详解
单向数据流与组件间通信 上文我们已经讲述过,react 单向数据流的原理和简单模拟实现.结合上文中的代码,我们来进行这节面试题的讲解: react中的组件间通信. 那么,首先我们把看上文中的原生js代 ...
- vue_组件间通信:自定义事件、消息发布与订阅、槽
自定义事件 只能用于 子组件 向 父组件 发送数据 可以取代函数类型的 props 在父组件: 给子组件@add-todo-event="addTodo" 在子组件: 相关方法中, ...
- Vue的父子组件间通信及借助$emit和$on解除父子级通信的耦合度高的问题
1.父子级间通信,父类找子类非常容易,直接在子组件上加一个ref,父组件直接通过this.$refs操作子组件的数据和方法 父 这边子组件中 就完成了父 => 子组件通信 2. 子 =&g ...
- React 组件间通信介绍
React 组件间通信方式简介 React 组件间通信主要分为以下四种情况: 父组件向子组件通信 子组件向父组件通信 跨级组件之间通信 非嵌套组件间通信 下面对这四种情况分别进行介绍: 父组件向子 ...
- Vue 根组件,局部,全局组件 | 组件间通信,案例组件化
一 组件 <div id="app"> <h1>{{ msg }}</h1> </div> <script src=" ...
- [转] React 中组件间通信的几种方式
在使用 React 的过程中,不可避免的需要组件间进行消息传递(通信),组件间通信大体有下面几种情况: 父组件向子组件通信 子组件向父组件通信 跨级组件之间通信 非嵌套组件间通信 下面依次说下这几种通 ...
随机推荐
- Jmeter系列(38)- 详解性能监控工具 nmon
如果你想从头学习Jmeter,可以看看这个系列的文章哦 https://www.cnblogs.com/poloyy/category/1746599.html 前言 做性能测试,服务器监控是至关重要 ...
- pigctf期末测评
pigctf期末测评 MISC 1 拿到图片,先binwalk一下,如下图 果然发现png图片后面跟了个ZIP,然后提取出来打开发现了一个flag.png,然后查看16进制文件没有发现什么问题,之后查 ...
- JVM 专题五:类加载子系统(三)补充内容
3. 补充内容 3.1 在jvm中表示两个class对象是否为同一个类的两个必要条件 类的完整类名必须一致,包括包名. 加载这个类的ClassLoader(指ClassLoader实例对象)必须相同 ...
- java 基本语法(十八)Lambda (五)Stream API
1.Stream API的理解:1.1 Stream关注的是对数据的运算,与CPU打交道集合关注的是数据的存储,与内存打交道 1.2 java8提供了一套api,使用这套api可以对内存中的数据进行过 ...
- web 部署专题(八):Nginx 反向代理中cookie相关问题
问题3:认证问题 Domino服务器中,通过写了一些接口代码,提供RESTful的服务,来对手机端进行提供服务.但是由于原来的环境,没有SSO,而且不通过认证,没法访问到Domino里面的接口代码. ...
- bzoj4396[Usaco2015 dec]High Card Wins*
bzoj4396[Usaco2015 dec]High Card Wins 题意: 一共有2n张牌,Alice有n张,Bob有n张,每一局点数大的赢.知道Bob的出牌顺序,求Alice最多能赢几局.n ...
- Linux下一只五颜六色的「猫」
大家好,我是良许. 有使用过 Linux 系统的小伙伴,肯定会使用过 cat 这个命令.当然,在 Linux 下,此猫非彼猫,这里的 cat 并不代表猫,而是单词 concatenate 的缩写. c ...
- Python 实现图像快速傅里叶变换和离散余弦变换
图像的正交变换在数字图像的处理与分析中起着很重要的作用,被广泛应用于图像增强.去噪.压缩编码等众多领域.本文手工实现了二维离散傅里叶变换和二维离散余弦变换算法,并在多个图像样本上进行测试,以探究二者的 ...
- .Net微服务实战之负载均衡(上)
系列文章 .Net微服务实战之技术选型篇 .Net微服务实战之技术架构分层篇 .Net微服务实战之DevOps篇 相关源码:https://github.com/SkyChenSky/Sikiro P ...
- Vue使用定时器定时刷新页面
1. 需求说明 在前端开发中,往往会遇到页面需要实时刷新数据的情况,给用户最新的数据展示. 2. 逻辑分析 如果需要数据实时更新,我们自然是需要使用定时器,不断的调用接口数据,会相对的消耗内存. 3. ...