Javascript 角度2主题不是函数
尝试与2个组件通信 我想我可以打一个http电话,然后也许合并映射或切换映射到一个主题 差不多Javascript 角度2主题不是函数,javascript,angular,Javascript,Angular,尝试与2个组件通信 我想我可以打一个http电话,然后也许合并映射或切换映射到一个主题 差不多 import {Subject} from 'rxjs/Subject'; constructor(private _http: HttpClient) { this.populateList = new Subject<Blog[]>(); } getBlogs(){ return this._http.get(this.blogsURL+'blogs')
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))
}
this.blogsService.populateList不是函数
我要寻找的是一种在http调用后更新视图的方法您需要像这样订阅,而不需要()。因为它不是一种功能。惊喜
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))
}
现在我得到“.do不是一个函数”,而且我以前以我展示的方式订阅过主题,没有问题您导入了“rxjs/add/operator/do”Ah是的,它对do函数起作用,但仍然得到了populateList error我希望您在服务中启动populateList:public populateList我现在看到了!!插入语,我一定累了…谢谢
getBlogs(){
return this._http.get(this.blogsURL+'blogs')
.map((result: Response ) => {
this.blogs = result['blogs'];
return this.blogs;
}).do((blogs)=>this.populateList.next(blogs))
}