ngx通讯之可观察对象实现
1.公共服务
//test.service.ts import {Injectable} from '@angular/core';
import {Subject} from 'rxjs/Subject'; @Injectable()
export class TestService{
missionAnnouncedSource = new Subject();
missionAnnounced$:any = this.missionAnnouncedSource.asObservable();
announceMission(misson:any){
this.missionAnnouncedSource.next(mission)
}
}
2.信息发出者
//test.component.ts (发送可观察对象)
import {TestService} from './test.service'; export class TestComponent implements OnInit {
constructor(private ts: TestService) { } ngOnInit(){
this.test();
} test(){ //发送信息
this.ts.announceMission("hello,it is just a test file")
}
}
3.信息接收者
//test2.component.ts (接收可观察对象)
import {TestService} from './test.service';
import {Subscription} from 'rxjs/Subscription'; export class Test2Component implements OnInit,OnDestory {
subscription:Subscription;
message:any;
constructor(private ts: TestService) {
this.subscription = ts.missionAnnounced$.subscribe(
mission => {
this.message = mission //接收信息
}
)
} ngOnInit(){ } ngOnDestory(){ //取消订阅
this.subscription.unsubscribe();
}
}
ngx通讯之可观察对象实现的更多相关文章
- wex5 教程 之 图文讲解 可观察对象的集群应用与绑定技术
一 前言: wex5官方教程里,开篇即以一个input输入,output即时输出的例子,为我们展现了一个概念:可观察对象.在以后我的项目开发中,将大量运用可观察对象. 那么,问题来了: 1. 可观察对 ...
- RxJS 简介:可观察对象、观察者与操作符
RxJS 简介:可观察对象.观察者与操作符 对于响应式编程来说,RxJS 是一个不可思议的工具.今天我们将深入探讨什么是 Observable(可观察对象)和 observer(观察者),然后了解如何 ...
- Angular2 Observable 可观察对象
可观察对象支持在应用中的发布者和订阅者之间传递消息.在需要进行事件处理,异步编程和处理多值的时候,可观察对象相对其他技术有显著的优点. 可观察对象是声明式的 —— 也就是说,虽然你定义了一个用于发布值 ...
- Angular的Observable可观察对象(转)
原文:https://blog.csdn.net/qq_34414916/article/details/85194098 Observable 在开始讲服务之前,我们先来看一下一个新东西——Obse ...
- [译]Rxjs&Angular-退订可观察对象的n中方式
原文/出处: RxJS & Angular - Unsubscribe Like a Pro 在angular项目中我们不可避免的要使用RxJS可观察对象(Observables)来进行订阅( ...
- Object.observe() 观察对象
这个对象方法可以用来异步观察对javascript对象的改动: // Let's say we have a model with data var model = {}; // Which we ...
- QPointer,QSharedPointer,QWeakPointer的区别与使用例子(QSharedPointer类似Delphi里的引用计数,是强引用,而QWeakPointer是弱引用,不影响原始对象的引用计数,相当于是在暗中观察对象,但保持联系,需要的时候就会出现)
QPointer is a template class that provides guarded pointers to Qt objects and behaves like a normal ...
- Vue,watch观察对象中的某个属性的变化
你只需要属性这样写,用引号引起来
- 可观察对象(Observable)
最简示例: export class AppComponent { title = 'angular-tour-of-heroes'; // Create an Observable that wil ...
随机推荐
- Unicode Category
Lu(letter,uppercase):大写字母. Ll(letter.lowercase):小写字母. Lt(letter,titlecase):词首字母大写的字母. Lm(letter,modi ...
- ubuntu 13.04 设定静态IP
切换到root用户,然后进入/etc/network目录.备份interfaces文件(备份文件是一个好习惯) 下面编辑interfaces文件,添加如下语句: # Assgin static IP ...
- 简单做出HTML5翻页效果文字特效
之前在网上看到一款比较有新意的HTML5文字特效,文字效果是当鼠标滑过是出现翻开折叠的效果,类似书本翻页.于是我兴致勃勃的点开源码看了一下,发现其实实现也挺简单的,主要利用了CSS3的transfor ...
- rail模型
rail是一种以用户为中心的性能模型,又google提出,其主要目标是让用户满意,主要分为response animation idle load四个部分 response 输入延迟时间小于100毫秒 ...
- 斯坦福机器学习视频笔记 Week3 逻辑回归与正则化 Logistic Regression and Regularization
我们将讨论逻辑回归. 逻辑回归是一种将数据分类为离散结果的方法. 例如,我们可以使用逻辑回归将电子邮件分类为垃圾邮件或非垃圾邮件. 在本模块中,我们介绍分类的概念,逻辑回归的损失函数(cost fun ...
- <Linux内核源码>文件系统VFS内核4.0.4版本基本概念源码
题外话:Linux内核从2.x和3.x到现在最新的4.x变化非常大,最直观的表现就是很多书上的内核代码已经无法直接继续使用,所以看看新的源码是非常有意义的! (下文中的内核源码都来自于 kernel ...
- C++(三)— 二维容器
1.二维bool向量 vector<vector<bool>> dp(len, vector<bool>(len, false));
- Python3 数据可视化之matplotlib、Pygal、requests
matplotlib的学习和使用 matplotlib的安装 pip3 install matplotlib 简单的折线图 import matplotlib.pyplot as plt #绘制简单的 ...
- Spring Boot -- actuator
Spring Boot有四大神器,分别是auto-configuration.starters.cli.actuator,本文主要讲actuator.actuator是spring boot提供的对应 ...
- Saiku_学习_02_Schema Workbench 开发mdx和模式文件
一.前言 saiku的查询都是通过cube来进行的.因此每当我们要进行一次多维度查询时,都要先修改xml.上传.重启才能生效,不仅效率低,还不利于学习和理解MDX和模式文件. 通过 workbench ...