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. 14.Django自带的admin配置

    admin有自己的默认显示,要自定义显示的样式,一般需要自己定义一个类,在自己定义的类里进行相应的设置,然后,把自己的类交给装饰器 交给装饰器的方法有两种: 1.@admin.register(Pub ...

  2. LeetCode:盛最多水的容器【11】

    LeetCode:盛最多水的容器[11] 题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为  ...

  3. c的详细学习(1)C语言概述

        本节用来简要介绍c语言.     (1)C语言的特点: C语言是一种集汇编语言及高级语言为一身的,面向过程的结构化和模块化的程序设计语言. 特点: 兼具高级语言与低级语言的双重能力.C语言允许 ...

  4. nginx expires缓存提升网站负载

    语法: expires [time|epoch|max|off]默认值: expires off作用域: http, server, location使用本指令可以控制HTTP应答中的“Expires ...

  5. mysql分区partition

    分区后 会产生多个 数据存储文件MYD,MYI ,把内容读取分散到多个文件上,这样减少并发读取,文件锁的概率,提高IO === 水平分区的几种模式:===1. Range(范围) – 这种模式允许DB ...

  6. In a Web Application and Mobile (hybrid), how to know which this platform running?

    needed depending on the platform to change the CSS to suit the size of the font. for example the DbG ...

  7. maven命令创建项目

    1)创建一个Project mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArti ...

  8. DBGrid和DBGridEH

    二.应用实例 Enlib3.0组件包安装成功后 A.定制标题行 1.制作复杂标题行 标题行可设为2行以上高度,并可以为多列创建一个共同的父标题行.为实现这个效果,需在各个列标题属性中以“|”分隔父标题 ...

  9. KbmMW-及相关

    KbmMW框架是收费的,不在此提供下载,如需购买,请自行联系作者Kim Madsen. 网址资源: 官网主页:http://www.components4programmers.com/product ...

  10. SetOperations

    无序集合,add的顺序不是存储顺序 1.add(K key, V value) 2.difference(K key, otherK[s]) :差集,返回Set 3.differenceAndStor ...