【发布时间】:2020-12-11 02:41:04
【问题描述】:
这是我的 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”依赖模块,但我似乎无法确定如何更正错误。 代码在浏览器上运行,但我没有得到想要的输出
【问题讨论】:
-
我确实杀死了我的服务器并重新启动它,但我仍然遇到同样的错误。我也检查了所有文件,我找不到任何错误。我还有其他方法可以使用吗?
标签: angular angular-material observable subscription subscribe