尝试与 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))
}
但是我得到:
You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array
我在尝试订阅主题时遇到错误:
this.blogsService.populateList()
.subscribe((res)=>{
console.log(res)
})
this.blogsService.populateList is not a function
我正在寻找一种在 http 调用后更新 View 的方法
最佳答案
你需要像这样订阅不带()。因为它不是一个函数。惊喜
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))
}
关于javascript - Angular 2主题不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46323515/