angular 2 通过服务共享数组

angular 2 share array through service

大家好,我正在尝试通过从组件 1 到组件 2 的服务在 Angular 2 中共享一个数组,但是我在这里什么也得不到是我的代码

我的服务

private employeeIdList = new Subject<any>();    
employeeIdList$ = this.employeeIdList.asObservable();

public setEmployeeList(employeeIdList){
    this.employeeIdList.next(employeeIdList);
}

从组件 1 我正在尝试这样做:

this.sharedService.setEmployeeList(this.employeeId);

this.employeId is an array of elements

当我尝试像这样访问它时

this.sharedService.employeeIdList$.subscribe(employeeList => {
  console.log(employeeList);
})

我一无所获,你能帮我解决这个问题吗

谢谢大家

我认为事件在接收者能够订阅之前发出是一个时间问题。要变通,请使用 BehaviorSubjectReplaySubject

private employeeIdList = new BehaviorSubject<any>();    

另一个可能的原因是您在多个地方提供服务,这可能会导致您获得多个实例,其中一个发出事件但接收者订阅另一个实例。