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通讯之可观察对象实现的更多相关文章

  1. wex5 教程 之 图文讲解 可观察对象的集群应用与绑定技术

    一 前言: wex5官方教程里,开篇即以一个input输入,output即时输出的例子,为我们展现了一个概念:可观察对象.在以后我的项目开发中,将大量运用可观察对象. 那么,问题来了: 1. 可观察对 ...

  2. RxJS 简介:可观察对象、观察者与操作符

    RxJS 简介:可观察对象.观察者与操作符 对于响应式编程来说,RxJS 是一个不可思议的工具.今天我们将深入探讨什么是 Observable(可观察对象)和 observer(观察者),然后了解如何 ...

  3. Angular2 Observable 可观察对象

    可观察对象支持在应用中的发布者和订阅者之间传递消息.在需要进行事件处理,异步编程和处理多值的时候,可观察对象相对其他技术有显著的优点. 可观察对象是声明式的 —— 也就是说,虽然你定义了一个用于发布值 ...

  4. Angular的Observable可观察对象(转)

    原文:https://blog.csdn.net/qq_34414916/article/details/85194098 Observable 在开始讲服务之前,我们先来看一下一个新东西——Obse ...

  5. [译]Rxjs&Angular-退订可观察对象的n中方式

    原文/出处: RxJS & Angular - Unsubscribe Like a Pro 在angular项目中我们不可避免的要使用RxJS可观察对象(Observables)来进行订阅( ...

  6. Object.observe() 观察对象

    这个对象方法可以用来异步观察对javascript对象的改动: // Let's say we have a model with data var model = {};   // Which we ...

  7. QPointer,QSharedPointer,QWeakPointer的区别与使用例子(QSharedPointer类似Delphi里的引用计数,是强引用,而QWeakPointer是弱引用,不影响原始对象的引用计数,相当于是在暗中观察对象,但保持联系,需要的时候就会出现)

    QPointer is a template class that provides guarded pointers to Qt objects and behaves like a normal ...

  8. Vue,watch观察对象中的某个属性的变化

    你只需要属性这样写,用引号引起来

  9. 可观察对象(Observable)

    最简示例: export class AppComponent { title = 'angular-tour-of-heroes'; // Create an Observable that wil ...

随机推荐

  1. hibernate 多对多 懒加载问题

    报错:org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: net. ...

  2. 使用微软官方U盘制作软件来安装纯净版windows

    第一步:下载一个制作U启的工具;windows-usb-dvd-download-tool 微软官网:https://www.microsoft.com/en-us/download/windows- ...

  3. 写出pythonic的python代码

    http://www.cnblogs.com/dadadechengzi/p/6226071.html 1,列表推导(没当要对序列中的内容进行循环处理时,就应该尝试使用列表推倒) 在python中编写 ...

  4. 基于Web的实验室管理系统技术简要报告

    基于Web的实验室管理系统技术简要报告 Copyright 朱向洋 Sunsea ALL Right Reserved 一.网站架构 该网站使用C#语言,利用SQL Server2008数据库,采用V ...

  5. location记录<18.7.21>

    // var index = location.href; // console.log(index) // // indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. // v ...

  6. cat echo 打印菜单

    cat << END        =============================        1.apple        2.pear        3.banana   ...

  7. 算法(Algorithms)第4版 练习 1.5.2

    0 1 2 3 4 5 6 7 8 9 10 components 9 0 0 1 2 3 4 5 6 7 8 9 components 3 4 0 1 2 4 5 6 7 8 0 8 compone ...

  8. AIM Tech Round 3 (Div. 2) A , B , C

    A. Juicer time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  9. 手把手编写PHP框架 深入了解MVC运行流程

    1 什么是MVC MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller ...

  10. appium-环境搭建(一)

    adb命令 adb的全称为Android Debug Bridge,就是起到调试桥的作用.借助adb工具,我们可以管理设备或者手机模拟器的状态.还可以进行很多手机操作,如安装软件\系统升级\运行she ...