1

在我返回的服务方法中,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;
      }
      });
  }

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

4

1 回答 1

8

您没有completed正确初始化主题

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

@Ompurdy演示

于 2017-07-22T21:10:46.267 回答