0

我正在尝试使用路由器导航更新查询参数

updateURL(queryParams){
        this.router.navigate(['path], {
            relativeTo: this.route,
            queryParams: queryParams,
            queryParamsHandling: 'merge',
            skipLocationChange: false
        });
}

/** Wont Work 
updateURL(queryParams);
updateURL(queryParams);
updateURL(queryParams);

/** Will Work
updateURL(queryParams);
// wait
updateURL(queryParams);
// wait
updateURL(queryParams)

当对该方法进行顺序调用以更新 URL 查询参数时,Angular 不会更新 url。如何处理它,以便即使在进行顺序调用时也能更新 url

4

1 回答 1

1

从 docs 保证你将不得不等待下一次更新

navigate(commands: any[], extras: NavigationExtras = { skipLocationChange: false }): Promise<boolean>

你能试试这个

async updateURL(queryParams){
       await this.router.navigate(['path], {
            relativeTo: this.route,
            queryParams: queryParams,
            queryParamsHandling: 'merge',
            skipLocationChange: false
        });
}

async UpdateQueryParam() {
 await updateURL(queryParams);
  await updateURL(queryParams);
  await updateURL(queryParams)
}

于 2020-08-20T08:25:10.880 回答