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 ...
随机推荐
- H5 Video 去除 下载按钮 禁用右键
<style type="text/css"> video::-webkit-media-controls-enclosure { overflow:hidden; } ...
- 关于highcharts-ng
1.内容都正确但是不显示,使用parseInt()方法转换
- 获取comboBox里面的item使用的方法
使用currentIndex()或者currentText() void Widget::calc() { int first = ui->firstLineEdit->text().to ...
- CVPR 2018:diractNets网络,有残差网络好吗?
我把我明天讲PPT的材料弄上来了........哈 哈哈
- 目标检测--之RCNN
目标检测--之RCNN 前言,最近接触到的一个项目要用到目标检测,还有我的科研方向caption,都用到这个,最近电脑在windows下下载数据集,估计要一两天,也不能切换到ubuntu下撸代码~.所 ...
- pyspark
http://www.aboutyun.com/thread-18150-1-1.html
- Datanode启动问题 FATAL org.apache.hadoop.hdfs.server.datanode.DataNode: Initialization failed for Block pool <registering>
-- ::, INFO org.apache.hadoop.hdfs.server.datanode.DataNode: supergroup = supergroup -- ::, INFO org ...
- MySQL 创建索引(Create Index)的方法和语法结构及例子
MySQL 创建索引(Create Index)的方法和语法结构及例子 MySQL 创建索引(Create Index)的方法和语法结构及例子 CREATE INDEX Syntax CREATE ...
- 广义表(C++实现)
广义表是非线性结构,其定义是递归的. 以下给出几种简单的广义表模型: 由上图我们可以看到,广义表的节点类型无非head.value.sub三种,这里设置枚举类型,利用枚举变量来记录每个节点的类型: e ...
- python正则-- re模块
匹配数字相关'.' 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行'^' 匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r" ...