Angular首先执行方法,然后导航

时间:2019-12-13 12:43:00

标签: angular typescript angular6

我想执行一个调用API的方法,因此一旦执行api,我想浏览URL。

当前,我已经尝试过以下代码:

this._router
      .navigate(valuationRoute)
      .then(data => { 
        if (!data) {
          this.getContent();
        }
      })

private getContent = async () => {
    try {
      this.isLoaded = false;
      localStorage.ReportDataStart = new Date();
      this.isRefreshing = true;
      this.spinnerService.show();

      if (this._fundBusinessId && this._fundPeriod && this._templateTypeId) {
        this.reportDataService.getContent(`val/api/${this._dealCode}/${this._fundPeriod}/${this._fundBusinessId}/${parseInt(this._templateTypeId)}`
        );
      }
    } catch (error) {
      this.isLoaded = false;
      this.fireRefreshComments = true;
      this.isRefreshing = false;
      this.spinnerService.hide();
      const errorMessage = this._customMessageService.formatError(error);
      this._alertService.error(
        this._messageService.displayReportError
      );
    }
  }

我想调用this.getcontent,并且在this.getcontent内,一旦我要导航的api获得结果,就会调用api。请帮助

1 个答案:

答案 0 :(得分:0)

只需在API响应后添加路线导航代码,如下所示:

this.reportDataService.getContent(//yourparams).then(() => {
    //put your navigation code here
    this.router.navigate(//routepath)
})