RxJs 主题错误 - 无法读取未定义的 属性 'subscribe'

RxJs Subject error - cannot read property 'subscribe' of undefined

在我的 returns Observable 服务方法中,我试图通过 Subject 通知组件操作已完成。

completed: Subject<boolean>

  constructor(private http: Http) {

  }

  loadItems(): Observable<FrontItemDto[]> {
    return this.http.get ( `${ServiceSettings.ApiUrl}/front` )
      .map ( res => {
        res.json ();
        if ( res.json () ) {
          this.completed.next ( true );
        }
      } )
      .catch ( (error: any) => Observable.throw ( error.json ().error || 'Server error' ) );
  }

组件是这样监听的Subject:

ngOnInit(): void {
    this.getItems();
    this.sub = this.dataService.completed.subscribe(completed => {
      if (completed) {
        this.show = false;
      }
      });
  }

但我收到一条错误消息,指出主题(已完成)未定义。我做错了什么?

您没有正确初始化completed主题

completed = new Subject<boolean>(); //it should have parenthesis at the end

Demo @Ompurdy