我不断收到 "Property subscribe does not exist on type void " 的错误
I keep getting error of "Property subscribe does not exist on type void "
这是我的 ts 文件函数,它抛出错误
post-list.componenet.ts
ngOnInit() {
this.posts = this.postsService.getPosts();
this.postsSub = this.postsService.getPostUpdateListener()
.subscribe((posts: Post[]) => {
this.posts = posts});
}
}
这是一个服务文件
@Injectable({providedIn: 'root'})
export class PostsService {
private posts: Post[] = [];
private postsUpdated = new Subject<Post[]>();
getPosts() {
return [...this.posts];
}
getPostUpdateListener(){
this.postsUpdated.asObservable();
}
addPosts(title: string, content: string) {
const post: Post = {title: title, content: content};
this.posts.push(post);
this.postsUpdated.next([...this.posts]);
}
}
我已经导入了 'rxjs' 依赖模块,但我似乎无法确定如何更正错误。
代码在浏览器上运行,但我没有得到所需的输出
不要忘记 return 您的数据到您的 getPostUpdateListener()
getPostUpdateListener(){
return this.postsUpdated.asObservable();
}
这是我的 ts 文件函数,它抛出错误
post-list.componenet.ts
ngOnInit() {
this.posts = this.postsService.getPosts();
this.postsSub = this.postsService.getPostUpdateListener()
.subscribe((posts: Post[]) => {
this.posts = posts});
}
}
这是一个服务文件
@Injectable({providedIn: 'root'})
export class PostsService {
private posts: Post[] = [];
private postsUpdated = new Subject<Post[]>();
getPosts() {
return [...this.posts];
}
getPostUpdateListener(){
this.postsUpdated.asObservable();
}
addPosts(title: string, content: string) {
const post: Post = {title: title, content: content};
this.posts.push(post);
this.postsUpdated.next([...this.posts]);
}
}
我已经导入了 'rxjs' 依赖模块,但我似乎无法确定如何更正错误。 代码在浏览器上运行,但我没有得到所需的输出
不要忘记 return 您的数据到您的 getPostUpdateListener()
getPostUpdateListener(){
return this.postsUpdated.asObservable();
}