Angular 如何console.log可观察对象的值?

Angular 如何console.log可观察对象的值?,angular,typescript,rxjs,Angular,Typescript,Rxjs,我正在使用angular 2和RxJS,我想知道如何做到以下几点: constructor( private store: Store<any> ) { this.count = this.store.select<any>(state => state.count); } 在我的组件中,我定义了以下内容: constructor( private store: Store<any> ) { this.cou

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

constructor(
    private store: Store<any>
  ) {
    this.count = this.store.select<any>(state => state.count);
  }
在我的组件中,我定义了以下内容:

constructor(
    private store: Store<any>
  ) {
    this.count = this.store.select<any>(state => state.count);
  }
计数:可观察

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

constructor(
    private store: Store<any>
  ) {
    this.count = this.store.select<any>(state => state.count);
  }
构造函数(
私人商店
) {
this.count=this.store.select(state=>state.count);
}

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

使用常规的可观察值,您只能在其更改时获取该值,因此如果您要console.log out该值,则需要console.log将其记录在订阅中:

constructor(
    private store: Store<any>
  ) {
    this.count = this.store.select<any>(state => state.count);
    this.count.subscribe(res => console.log(res));
  }
构造函数(
私人商店
) {
this.count=this.store.select(state=>state.count);
subscribe(res=>console.log(res));
}
但是,如果您希望能够在任何时候获得当前值,那么您需要的是一个BehaviorSubject(它在函数中结合了一个Observable和一个Observable…像Observable一样从rxjs库导入它)

private count:BehaviorSubject=new BehaviorSubject(0);
建造师(
私人商店
) {
让自我=这个;
select(state=>self.count.next(state.count));
}

然后,每当您想要获取计数的当前值时,您都会调用
this.count.getValue()
来更改您将调用
this.count.next()
的值。这应该可以满足您的需求。

对于RxJS的最新版本(AFAIR从6.0开始),正确的方法是使用
.pipe()
。要回答您的问题,您需要点击操作员

constructor(
    private store: Store<any>
  ) {
    this.count = this.store.select<any>(state => state.count).pipe(
        tap(countValue => console.log('Count: ', countValue))
    );
  }
构造函数(
私人商店
) {
this.count=this.store.select(state=>state.count).pipe(
轻触(countValue=>console.log('Count:',countValue))
);
}

添加日志记录的最快方法是将
.pipe(点击(console.log))
放在您的可观察对象之后(在本例中,放在
this.store.select之后):


没有退订,很可能是内存泄漏。你是如何在rxjs的控制台中获得所有这些颜色的?哦,是日志记录工具,nvm。