获取Observable的值或BehaviourSubject Angular2的值 [英] Get value of Observable or value of BehaviourSubject Angular2

查看:818
本文介绍了获取Observable的值或BehaviourSubject Angular2的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Observable(variable1) BehaviourSubject(variable2),我想同时获得两者的.

I have Observable(variable1) and BehaviourSubject(variable2) and I want to get values of both.

variable1.value
variable2.value

不能正常工作,我收到一条消息可观察"类型上不存在属性值.您能否告诉我如何获得价值?(澳大利亚的轮胎")

are not working, I get a message Property value doesn't exist on type 'Observable'. Could you please tell me how could I get value? ('Tiere in Australia')

推荐答案

Observable将在订阅时返回其包含的所有值

Observable will return all values that it contains when subscribed

// creating an Observable
source = Observable.of(1,2,3);

this.source.subscribe(item=>{
  this.values.push(item)
  console.log('observable',this.values)

})

BehaviourSubject每次订阅只会返回一个值

BehaviourSubject will return only one value per subscription

// creating an BehaviourSubject
subject = new BehaviorSubject(56,121);

this.subject.subscribe(item=>console.log(item))

实时演示

LIVE DEMO

这篇关于获取Observable的值或BehaviourSubject Angular2的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆