获取将由router.navigate生成的URL

gar*_*hdn 4 typescript angular angular-router

我不知道是否有一种方式来获得该URL 被角的路由器调用时产生的navigate方法.

例如:

this._router.navigate(['browse'], { queryParams: { section: 'users', isActive: true } });

可能会导致导航 /browse#?section=users&isActive=true

如何在不执行导航或更新浏览器中的URL的情况下获取此路线?

Kir*_*kin 7

看看createUrlTree:

this._router.createUrlTree(
    ['browse'], { queryParams: { section: 'users', isActive: true } });
Run Code Online (Sandbox Code Playgroud)

返回的对象,a UrlTree有一个toString应该给你你想要的功能.

您可以从内部实际使用的中看到:navigatecreateUrlTree

navigate(commands: any[], extras: NavigationExtras = {skipLocationChange: false}):
        Promise<boolean> {
    validateCommands(commands);
    return this.navigateByUrl(this.createUrlTree(commands, extras), extras);
}
Run Code Online (Sandbox Code Playgroud)