使用angular2中的其他组件的可观察对话,不接收即将到来的值

时间:2016-08-10 18:46:11

标签: javascript angularjs angular rxjs angular2-observables

我有这个结构

app/
  --pages/
  ----pages.component.ts 
  --channel/
  ----shared/
  ------assignPlaylist.modal.ts

在pages.component.ts中,我有一个名为播放列表的变量

export class PagesComponent {

  @Input() platform: Platform;

  @Output() onPlaylistsChange: EventEmitter<Playlists>;

  currentPageName: string;

  currentPage: Page;

  pages: Array<Page>;

  playlists: Playlists;
}

在assignPlaylist.modal.ts中,我制作一个http post方法并返回一个新的播放列表,我需要使用返回的播放列表来替换pages.component.ts中的播放列表

this.apiService.addPlaylistToPage(playlistId, stDate, etDate, this.apiService.getGlobalRegion(), callback)
          .subscribe(
            data => {
              if (this.apiService.g_platform == 'DESKTOP') {
                this.apiService.getPlaylist(this.apiService.getCurrentPage(), 'true' )
                .subscribe(
                  res => {                          this.pagesService.setCurrentPlaylists(res.playlists);
                  }
                );
            } else {
              this.apiService.getPlaylist(this.apiService.getCurrentPage(), 'false' )
              .subscribe(
                res => {           this.pagesService.setCurrentPlaylists(res.playlists);
                }
              );
            }
            }
          );

这是res,返回结果enter image description here

这是播放列表的结构

export class Playlists {
  headerPlaylists: Array<Playlist>;
  bodyPlaylists: Array<Playlist>;
  wholePlaylists: Array<Playlist>;
}

----------------更新---------------------- 正如答案中所提到的,我做了以下更改。

@Injectable()
export class PagesService {

  private currentPlaylists: Subject<Playlists> = new BehaviorSubject<Playlists>(new Playlists());
  getCurrentPlaylists() {
    return this.currentPlaylists.asObservable();
  }

  setCurrentPlaylists(playlists: Playlists) {
    console.log('i am here');
    this.currentPlaylists.next(playlists);
  }
}

enter image description here

控制台显示&#39;我在这里&#39;

我写在我的pages.component.ts

 constructor(private service: PagesService, private playlistService: PlaylistService) {
    this.pages = [];
    this.currentPage = new Page();
    this.service.setCurrentPage(this.currentPage);
    this.playlists = new Playlists();
    this.onPlaylistsChange = new EventEmitter<Playlists>();
    this.service.getCurrentPlaylists().subscribe((playlists) => {
      console.log(playlists, 'new playlists coming');
      this.playlists = playlists;
    }, error => {
      console.log(error);
    });
  }

但是,我没有看到消息&#39;新的播放列表即将到来,我使用它是错误的吗?

新更新: 我检查了这个。播放列表,它的观察者是0,为什么?

1 个答案:

答案 0 :(得分:2)

创建一个PlaylistService,公开PagesComponent可以订阅的可观察对象。

import { Injectable} from '@angular/core';
import { Subject,BehaviorSubject } from 'rxjs';
import {Playlists} from 'channel' /** Assumes this is where you have defined your Playlists interface **/

@Injectable()
export class PlaylistService {
    private _currentPlaylists$: Subject<Playlists> = new BehaviorSubject<Playlists>(null);
    constructor() {}

    currentPlaylists() {
      return this._currentPlaylists$.asObservable();
    }

    setCurrentPlaylists(playlists:Playlists){
      this._currentPlaylists$.next(playlists);
    }
}

然后将此服务导入您的pagescomponent并订阅播放列表,如下所示: -

this.playlistService.currentPlaylists().subscribe((playlists) => {
      this.playlists = playlists;
},error => {
    console.log(error);
});`

请记住,您还必须将此服务导入您的assignPlaylistModal并使用它如下: -

   this.apiService.addPlaylistToPage(playlistId, stDate, etDate, this.apiService.getGlobalRegion(), callback)
      .subscribe(
        data => {
          if (this.apiService.g_platform == 'DESKTOP') {
            this.apiService.getPlaylist(this.apiService.getCurrentPage(), 'true' )
            .subscribe(
              res => {
                this.playListService.setCurrentPlaylists(res);
              }
            );
        } else {
          this.apiService.getPlaylist(this.apiService.getCurrentPage(), 'false' )
          .subscribe(
            res => {
              this.playListService.setCurrentPlaylists(res);
            }
          );
        }
        }
      ); 

请阅读有关observables的更多信息,以了解为何使用BehaviourSubject。 http://reactivex.io/documentation/observable.html