我如何console.log可观察的值?

时间:2021-07-02 16:55:34

I am using angular 2 and RxJS, and I am wondering how i can do the following:

我正在使用角度2和RxJS,我想知道我如何能够做到以下几点:

In my component, I have defined the following:

在我的组件中,我定义了以下内容:

count: Observable<number>;

count:Observable ;

In my component's constructor, I am doing the following:

在我的组件的构造函数中,我正在执行以下操作:

constructor(
    private store: Store<any>
  ) {
    this.count = this.store.select<any>(state => state.count);
  }

How can I view the current value for the count? Right now if I console.log(this.count) I get a big object to log. If I want to view just the value for this.count, how can I do that?

如何查看计数的当前值?现在,如果我是console.log(this.count),我会得到一个大对象来记录。如果我只想查看this.count的值,我该怎么做?

1 个解决方案

#1


12  

With a regular observable you only get the value when it changes, so if you want to console.log out the value you will need to console.log it in the subscription:

使用常规observable,只有在更改时才能获得值,因此如果您想要console.log输出您需要在订阅中使用console.log的值:

constructor(
    private store: Store<any>
  ) {
    this.count = this.store.select<any>(state => state.count);
    this.count.subscribe(res => console.log(res));
  }

However if you are wanting to be able to get the current value at any time what you will be wanting is a BehaviorSubject (which combines an Observable and an Observer in function...import it from the rxjs library like you do Observable).

但是,如果您希望能够随时获得当前值,那么您将需要的是一个BehaviorSubject(它将函数中的Observable和Observer组合在一起......从像Observable一样从rxjs库中导入它)。

private count:BehaviorSubject<number> = new BehaviorSubject<number>(0);

constructor(
    private store: Store<any>
  ) {
    let self = this;
    self.store.select<any>(state => self.count.next(state.count));
  }

Then any time you want to get the current value of the count you would call this.count.getValue() to change the value you would call this.count.next(<the value you want to pass in>). That should get you what you are looking for.

然后,只要你想获得计数的当前值,你就可以调用this.count.getValue()来改变你将调用this.count.next( <你要传入的值> )的值。这应该可以帮到你找到你想要的东西。

#1


12  

With a regular observable you only get the value when it changes, so if you want to console.log out the value you will need to console.log it in the subscription:

使用常规observable,只有在更改时才能获得值,因此如果您想要console.log输出您需要在订阅中使用console.log的值:

constructor(
    private store: Store<any>
  ) {
    this.count = this.store.select<any>(state => state.count);
    this.count.subscribe(res => console.log(res));
  }

However if you are wanting to be able to get the current value at any time what you will be wanting is a BehaviorSubject (which combines an Observable and an Observer in function...import it from the rxjs library like you do Observable).

但是,如果您希望能够随时获得当前值,那么您将需要的是一个BehaviorSubject(它将函数中的Observable和Observer组合在一起......从像Observable一样从rxjs库中导入它)。

private count:BehaviorSubject<number> = new BehaviorSubject<number>(0);

constructor(
    private store: Store<any>
  ) {
    let self = this;
    self.store.select<any>(state => self.count.next(state.count));
  }

Then any time you want to get the current value of the count you would call this.count.getValue() to change the value you would call this.count.next(<the value you want to pass in>). That should get you what you are looking for.

然后,只要你想获得计数的当前值,你就可以调用this.count.getValue()来改变你将调用this.count.next( <你要传入的值> )的值。这应该可以帮到你找到你想要的东西。