首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从角度8中可观测到的最后值

从角度8中可观测到的最后值
EN

Stack Overflow用户
提问于 2020-02-13 08:11:12
回答 2查看 17.1K关注 0票数 4

如何从角度8的可观测值中得到最后一个值?

代码语言:javascript
复制
let test = new BehaviorSubject<any>('');
test.next(this.AddressObservable);

let lastValue = test.subscribe(data=>console.log(data.value));

由于某些原因,它不工作,检查调试器。

然而,这在html,AddressObservable | async中是有效的。

尝试使用此链接时,希望将值存储在变量中或显示在控制台日志中。任何新的语法都会有帮助。

How to get last value when subscribing to an Observable?

注:

AddressObservable类型为:Observable<AddressDto>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-13 08:24:54

你有点混淆了你的行为主题,以及它对你有什么好处。

主题实际上是来自您可以订阅的某个源的值流。每当发出新值时,它就会到达您的订阅方法中。

如果您具有初始状态,则可以使用行为主题初始化主题,并确保新订阅者始终收到值。

如果您没有初始状态,但希望确保新订阅者在订阅时获得最后发出的值(如果存在的话),则可以使用重播主题。

进入主题的所有值都是T类型的,其中有Subject<T>。因此,在您的例子中,进入主题的所有内容都应该是AddressDto

如果您有一个初始地址,那么您将设置如下行为主题:

代码语言:javascript
复制
// somehow get my initial address
const address = new AddressDto(); //
const test = new BehaviorSubject<AddressDto>(address);

// all subscribers will receive this address upon subscribing

// ... some things happen

// now I have another address, emit that
const newAddress = new AddressDto();
test.next(newAddress);

// all new subscribers will now receive newAddress upon subscribing

另一方面,如果您没有初始地址,则可以使用这样的重播主题:

代码语言:javascript
复制
// always emit the last address to new subscribers by intitialing with a 1
// New subscribers won't receive an address until one is emitted
const test = new ReplaySubject<AddressDto>(1);

// ... some things happen

// now I have my first address, emit that
const firstAddress = new AddressDto();
test.next(firstAddress);

// all current subscribers receive firstAddress
// all future subscribers will receive firstAddress upon subscribing

// ... some things happen

const secondAddress = new AddressDto();
test.next(secondAddress);

// all current subscribers receive secondAddress
// all future subscribers will now receive secondAddress upon subscribing

编辑:

您已经询问过如何将最后一个值存储在变量中。你所说的意思是模棱两可的,所以我假设你指的是源头,因为这更复杂。

一旦你开始思考主语/可观察性的概念,你就会开始进入可观察管道的概念。各种事情都可能发生在管道中--只要把它想象成一系列可以发生在对象上的步骤,就像普通javascript数组上的一系列链式数组函数一样。

在管道中可以做的事情之一是在tap()操作符中执行“副作用”。这就意味着,您可以在管道中间做一些事情,同时让数据通过。其中之一可以是将值存储在变量(或本地存储或其他什么)中,以达到某种目的。

如果您控制了主题的内容,那么在管道中这样做似乎是多余的,所以我将使用缓存http请求结果的示例。

代码语言:javascript
复制
this.http.get(url).pipe(
  // transform the http response into an object you have created
  map(response => this.mapResponseToMyClass(response)),
  // store the mapped object in a local property for later use
  tap(myClass => {
    // you can perform any side-effect actions you want here
    console.log(myClass);
    // store the value in a variable
    this.cachedMyClass = myClass;
  })
);

你自己的主题没有什么不同--进入主题的所有东西都会通过管道,然后输出到订阅者那里。

代码语言:javascript
复制
private subject = new Subject<AddressDto>();

getPostcode(): Observable<string> {
  // reuse the local subject. All subscribers to this function will receive addresses that have come through the pipe.
  return subject.pipe(
    map(address => address.postcode),
    // store the last postcode in a local property
    tap(postcode => this.lastPostcode = postcode)
    // the postcode still comes out here to all subscribers
  ).asObservable();
}
票数 9
EN

Stack Overflow用户

发布于 2020-02-13 08:24:21

可观察并不能储存价值。在将可观测流中的数据推送到接收值之前,您应该订阅“可观测”。我认为你的AddressObservable是一个可观察的,而不是BehaviorSubject。如果您将AddressObservable转换为BehaviorSubject。看起来不错。如果是AsyncPipe,它可以工作,因为它的异步管道订阅了相同的内容。

将AddressObservable更改为类型: BehaviorSubject

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60203165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档