角度2主题不是一个功能

时间:2017-09-20 13:32:24

标签: javascript angular

尝试与2个组件通信 我以为我能够进行http调用,然后将mergeMap或switchMap转换为主题?

这样的东西
import {Subject} from 'rxjs/Subject';

 constructor(private _http: HttpClient) {
    this.populateList = new Subject<Blog[]>();
 }

getBlogs(){
  return this._http.get(this.blogsURL+'blogs')
       .map((result: Response ) => {
       this.blogs = result['blogs'];
    return this.blogs;
  }).switchMap((blogs)=>this.populateList.next(blogs))
}

但我明白了:

  

您提供了&#39; undefined&#39;预期流的地方。你可以提供   一个Observable,Promise,Array

我只是在尝试订阅主题时遇到错误:

 this.blogsService.populateList()
      .subscribe((res)=>{
      console.log(res)
  })
  

this.blogsService.populateList不是函数

我正在寻找的是一种在http调用后更新视图的方法

1 个答案:

答案 0 :(得分:1)

你需要在没有()的情况下订阅。因为它不是一个功能。惊喜

 this.blogsService.populateList.subscribe()

并重写这样的第一个函数导致你不需要切换映射你只需要做副作用来填充列表。

getBlogs(){
  return this._http.get(this.blogsURL+'blogs')
       .map((result: Response ) => {
       this.blogs = result['blogs'];
    return this.blogs;
  }).do((blogs)=>this.populateList.next(blogs))
}