小编kum*_*umo的帖子

使用带有RxJS的Angular 5可以看到带有过滤器的数组

我正在创建一个简单的论坛。

我想过滤帖子。我在RxJS中使用.pipe和.filter遇到了一些麻烦。

我试图:

  • 从中检索in-memory-api帖子列表api/posts,当与http.get一起使用时,它返回一个Observable<Post[]>
  • 重复遍历Post中的Observable<Post[]>每个帖子并对其进行过滤,以便仅选择ID为1(每个ID的帖子1)的每个帖子。
  • 将任何错误或功能成功登录到控制台

filter不选择在每个单独的片Post[]阵列,而是选择Post[]本身。

我的代码:

getPosts(): Observable<Post[]> {

  // Sets the url to the service's postsUrl
  const url = this.postsUrl;

  // The http.get returns an Observable<Post[]>
  return this.http.get<Post[]>(url)
    .pipe(
      filter(
        post: Post => post.id == 1
      ),
      // Log posts were fetched
      tap(posts => console.log('Posts fetched.')),
      // Error handling
      catchError(this.handleError('getPosts', []))
    );
 }
Run Code Online (Sandbox Code Playgroud)

我的错误:

Property 'id' does not exist on …
Run Code Online (Sandbox Code Playgroud)

rxjs typescript angular angular5

1
推荐指数
1
解决办法
2966
查看次数

标签 统计

angular ×1

angular5 ×1

rxjs ×1

typescript ×1