【问题标题】:I keep getting error of "Property subscribe does not exist on type void "我不断收到“类型 void 上不存在属性订阅”的错误
【发布时间】: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


【解决方案1】:

不要忘记在 getPostUpdateListener() 中返回您的数据

getPostUpdateListener(){
   return this.postsUpdated.asObservable();
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 2018-01-29
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多