简体   繁体   English

Angular2 / 4-创建数据数组以通过服务在多个组件之间共享

[英]Angular2/4 - Creating an array of data to share across multiple components via service

Overview: 概述:

I have a UI that allows a user to select one or more employees based on various search criteria. 我有一个UI,允许用户根据各种搜索条件选择一个或多个员工。 When they select them, I need to store the selected employees in an array, within my shared service. 当他们选择他们时,我需要将选择的雇员存储在我的共享服务中的一个阵列中。

Before any of this data is sent to the server, the array could be modified by adding more employees or removing some that exist in the array. 在将这些数据中的任何一个发送到服务器之前,可以通过添加更多雇员或删除阵列中存在的某些雇员来修改阵列。

I need to be able to create and subscribe to an array of data in this shared service. 我需要能够在此共享服务中创建和订阅数据数组。

My Approach: 我的方法:

My initial approach was to use a BehaviorSubject so that I could call next and pass the data along when needed. 我最初的方法是使用BehaviorSubject以便可以调用next并在需要时传递数据。 This became an issue though because I didn't have a way to see all of the stored/selected users, only the last one that was passed through the BehaviorSubject . 但是,这成为一个问题,因为我没有办法查看所有已存储/选定的用户,只有最后一个通过BehaviorSubject传递的用户。

Psuedo Code: 伪代码:

shared.service.ts 共享服务

public selectedUsers = [];  //<- How do I store stuff in here?

private selectedUsersSub = new BehaviorSubject<any>(null);
selectedUsers$ = this.selectedUsersSub.asObservable();

setSelectedUsers(data) {
  this.selectedUsersSub.next(data);
}

get selectedUsers(){
  return this.selectedUsers;
}

component.ts: component.ts:

this._reqService.selectedUsers$.subscribe(
      data => {
        if (data) {
          console.log('Observable Stream', data)
        }
      } 
    )

My goal here is to be able to store my selected employees in this selectedUsers array. 我的目标是能够将我选择的员工存储在这个selectedUsers数组中。 My other components need to be able to subscribe so that they are always up-to-date with the current value of selectedUsers . 我的其他组件需要能够订阅,以便它们始终与selectedUsers的当前值保持最新。

I also need to be able to access the current array of selected users at any time, not just the last value. 我还需要能够随时访问选定用户的当前数组,而不仅仅是最后一个值。

Delete public selectedUsers = []; 删除public selectedUsers = [];

delete get selectedUsers(){ return this.selectedUsers; } delete get selectedUsers(){ return this.selectedUsers; } get selectedUsers(){ return this.selectedUsers; }

And in any component you want to fetch the selectedUsers just subscribe to the public observable selectedUsers$ 在您要获取selectedUsers的任何组件中,只需订阅公共可观察的selectedUsers$

in a component this.subscription = this.yourService.selectedUser$.subscribe((users)=>//do stuff here like push the users to the users array of the component) 在组件中this.subscription = this.yourService.selectedUser$.subscribe((users)=>//do stuff here like push the用户this.subscription = this.yourService.selectedUser$.subscribe((users)=>//do stuff here like push the to the users array of the component)

The service needs to be inject to a shared module in order all the components to get the same state (data). 该服务需要注入到共享模块中,以便所有组件都获得相同的状态(数据)。

More details: https://angular.io/guide/component-interaction#parent-and-children-communicate-via-a-service 更多详细信息: https : //angular.io/guide/component-interaction#parent-and-children-communicate-via-a-service

Your approach is wrong here. 您的方法在这里是错误的。 You have 2 basic options in a shared service pattern. 共享服务模式中有2个基本选项。 1 is to use a store pattern where you have a predefined set of data manipulations and use the scan operator, this is more complex, the simpler is to pass the entire list every time you want to update the list. 第一种是使用存储模式,在该模式中您具有一组预定义的数据操作并使用扫描运算符,这更复杂,更简单的方法是每次您要更新列表时都传递整个列表。

So your components will not only send the update, they'll first get the entire list and then manipulate and then send it. 因此,您的组件不仅将发送更新,而且还将首先获取整个列表,然后进行操作然后将其发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM