Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用数据存储和主题行为时更新回调(7)_Angular_Typescript_Rxjs_Angular7 - Fatal编程技术网

Angular 使用数据存储和主题行为时更新回调(7)

Angular 使用数据存储和主题行为时更新回调(7),angular,typescript,rxjs,angular7,Angular,Typescript,Rxjs,Angular7,第一个帖子。我是个新手。我对此做了一些调查,但没有找到答案 在此之前,我直接订阅了get/post/put函数,这些函数联系了API并返回了值。这样做时,我可以直接在GUI中处理回调 这种方法不是最好的,因为我需要在多个组件之间共享数据 我最终创建了数据存储。现在的问题是,在执行例如:update之后,我仍然需要回调 我怎样才能做到这一点 谢谢 在职: private _projects: BehaviorSubject<Project[]>; private serviceUrl

第一个帖子。我是个新手。我对此做了一些调查,但没有找到答案

在此之前,我直接订阅了get/post/put函数,这些函数联系了API并返回了值。这样做时,我可以直接在GUI中处理回调

这种方法不是最好的,因为我需要在多个组件之间共享数据

我最终创建了数据存储。现在的问题是,在执行例如:update之后,我仍然需要回调

我怎样才能做到这一点

谢谢

在职:

private _projects: BehaviorSubject<Project[]>; 
private serviceUrl = `${this.baseUrl}/api/project`;

private dataStore: { 
    projects: Project[]
};

get projects() {
    return this._projects.asObservable();
}

constructor(private http: HttpClient, public listener: ProjectListenerService, public authService: AuthService) {
    super();

    this.dataStore = { projects: [] };
    this._projects = <BehaviorSubject<Project[]>>new BehaviorSubject([]);
}

public loadAll() {
    return this.http.get<Project[]>(this.serviceUrl).pipe(
        catchError(this.handleError),
        map(res => res.map(v => new Project(v)))
    ).subscribe(data => {
        this.dataStore.projects = data;
        this._projects.next(Object.assign({}, this.dataStore).projects);
    }, error => console.log('Could not load projects.'));
}

public update(project: Project) {
    let payLoad = { Data: project, ConnectionId: this.listener.ClientConnectionId, HubAction: "AllExcept" };

    this.http.put(`${this.baseUrl}/${project.id}`, payLoad).subscribe(data => {
      if (typeof data == typeof true) {
        this.dataStore.projects.forEach((t, i) => {
          if (t.id === project.id) { this.dataStore.projects[i] = project; }
        });

        this._projects.next(Object.assign({}, this.dataStore).projects);
      } else console.log('Could not update project.');
    }, error => console.log('Could not update project.'));
}

我认为最简单的方法是添加另一个要更新的参数,即在此之后调用的回调。\u projects.next

一个更好的Rx解决方案是使用share或shareReplay并返回共享的可观察对象

public update(project: Project) {
  const shared = this.http.put(...).pipe(
    shareReplay(),
  );

  shared.subscribe(data => {
    ...
  });

  return shared;
}
然后,您可以订阅返回的Observable并根据需要更新GUI

this.projectService.update(project).subscribe(...);

你看过NgRx了吗?它是Angular开发人员用于创建和管理应用程序存储和状态的设计模式。你可以在这里找到更多关于它的信息:我没有!谢谢你的链接。将阅读更多关于NgRx的内容。谢谢!我将使用第一个选项。当尝试使用第二个更多的Rx解决方案时,我没有获得订阅共享obj的功能。它只有取消订阅、添加和删除。是否确实尝试返回共享;并且不返回共享。订阅。。。?这是两件不同的事情。
this.projectService.update(project).subscribe(...);