【问题标题】:Get Key/Value Data from Array of Objects with BehaviorSubject使用 BehaviorSubject 从对象数组中获取键/值数据
【发布时间】:2018-03-14 15:34:19
【问题描述】:

我看到人们推荐 BehaviorSubject 使用 getValue() 获取其当前值。但是,当我将它与我的对象数组一起使用时,控制台记录的所有内容都是“Observable {_isScalar: false, source: Observable, operator: MapOperator}”,下拉箭头指向除我需要的数据之外的所有内容。我觉得我错过了一些东西,但在寻找了几个小时后我仍然迷路了。我错过了什么?

示例代码:

defaultData = this.http.get(this.dataUrl);
defaultProducts = new BehaviorSubject<any>(this.defaultData);
products = this.defaultProducts.asObservable();

getData() {
    let test = this.defaultProducts.getValue();
    console.log(test);
    return this.http.get(this.dataUrl);
}

【问题讨论】:

  • 请提供您获取该日志的代码。

标签: observable behaviorsubject


【解决方案1】:

我看到了这篇关于使用 RXJS 从 observable 中提取值的文章。我仍在试图弄清楚。

 canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | boolean {

      return this.auth.user
           .take(1)
           .map(user => !!user)
           .do(loggedIn => {
             if (!loggedIn) {
               console.log('access denied')
               this.router.navigate(['/login']);
             }
         })

  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多