第一个http请求在第二个之后结束。如何解决?

时间:2019-05-03 16:31:19

标签: angular http asynchronous web-applications

我的应用程序有一些生命周期,如下所示:

  1. 获取请求数据。
  2. 更改数据。
  3. 放入请求数据。
  4. 获取所有数据。
  5. 显示所有数据。
  6. 转到第1点。

问题是,点3在点1结束之前开始,点4在点4结束之前开始。 代码位于不同的文件中。

文件1.针对http请求的服务

getObserver() {
        return this.http.get(this.urlAddress);
}

setTitleForId(
        this.http.put(this.urlAddress, obj).subscribe();
    }

文件2组件,使用文件1。

  ngOnInit() {
    this.httpService.getObserver().subscribe((resp) => {
      this.obj = resp[this.id] as Obj;
    });
  }

 changeTitle(newTitle: string) {
    this.obj.title = newTitle;
    this.httpService.setTitleForId(this.obj);
  }

文件3。这是第二个组件

ngOnInit() {
    this.objs= [];
    this.httpService.getMemesObserver().subscribe((resp: Obj[]) => {
      this.objs= resp;
    });
  }

Obj为:

export class Obj{
    id: number;
    title?: string;
}

这是我第五次参加角球比赛。 也许存在信号量之类的东西?

0 个答案:

没有答案