gpt4 book ai didi

javascript - Angular 2主题不是函数

转载 作者:行者123 更新时间:2023-11-30 21:08:59 27 4
gpt4 key购买 nike

尝试与 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com