如何在Angular中使用router.navigateByUrl和router.navigate

Mic*_*ael 30 routes routerlink angular

https://angular.io/api/router/RouterLink很好地概述了如何创建链接,将用户带到Angular4中的不同路径,但是我找不到如何以编程方式执行相同的操作而不需要用户单击链接

Max*_*kyi 65

navigateByUrl

routerLink 像这样使用的指令:

<a [routerLink]="/inbox/33/messages/44">Open Message 44</a>
Run Code Online (Sandbox Code Playgroud)

只是命令式导航使用router及其navigateByUrl方法的包装:

router.navigateByUrl('/inbox/33/messages/44')
Run Code Online (Sandbox Code Playgroud)

从资料来源可以看出:

export class RouterLink {
  ...

  @HostListener('click')
  onClick(): boolean {
    ...
    this.router.navigateByUrl(this.urlTree, extras);
    return true;
  }
Run Code Online (Sandbox Code Playgroud)

因此,只要您需要将用户导航到另一个路径,只需注入router和使用navigateByUrl方法:

class MyComponent {
   constructor(router: Router) {
      this.router.navigateByUrl(...);
   }
}
Run Code Online (Sandbox Code Playgroud)

导航

您可以使用路由器上的另一种方法 - 导航:

router.navigate(['/inbox/33/messages/44'])
Run Code Online (Sandbox Code Playgroud)

两者之间的差异

使用router.navigateByUrl类似于直接更改位置栏 - 我们提供"整个"新URL.而 router.navigate通过将传入命令(补丁)数组应用于当前URL来创建新URL.

要清楚地看到差异,请想象当前的URL是 '/inbox/11/messages/22(popup:compose)'.

使用此URL, router.navigateByUrl('/inbox/33/messages/44')将导致 '/inbox/33/messages/44'调用,并且router.navigate(['/inbox/33/messages/44'])将导致 调用 '/inbox/33/messages/44(popup:compose)'.

阅读官方文档中的更多内容.


Eug*_*hov 12

router.navigate 与 router.navigateByUrl

router.navigate只是一种方便的包装方法,router.navigateByUrl归结为:

navigate(commands: any[], extras) {
    return router.navigateByUrl(router.createUrlTree(commands, extras), extras);
}
Run Code Online (Sandbox Code Playgroud)

正如其他答案中提到的,router.navigateByUrl只接受绝对 URL:

// This will work
router.navigateByUrl("http://localhost/team/33/user/11")
// This WON'T work even though relativeTo parameter is in the signature
router.navigateByUrl("../22", {relativeTo: route})
Run Code Online (Sandbox Code Playgroud)

所有的相对计算都由router.createUrlTree和完成router.navigate。数组语法用于将每个数组元素视为 URL 修改“命令”。例如".."- 上升,"path"- 下降,{expand: true}- 添加查询参数等。您可以像这样使用它:

// create /team/33/user/11
router.navigate(['/team', 33, 'user', 11]);

// assuming the current url is `/team/33/user/11` and the route points to `user/11`

// navigate to /team/33/user/11/details
router.navigate(['details'], {relativeTo: route});

// navigate to /team/33/user/22
router.navigate(['../22'], {relativeTo: route});

// navigate to /team/44/user/22
router.navigate(['../../team/44/user/22'], {relativeTo: route});
Run Code Online (Sandbox Code Playgroud)

{relativeTo: route}参数很重要,因为这是路由器将用作相关操作的根的参数。

通过组件的构造函数获取它:

  // In my-awesome.component.ts:

  constructor(private route: ActivatedRoute, private router: Router) {}
  
  // Example call
  onNavigateClick() {
    // Navigates to a parent component
    this.router.navigate([..], { relativeTo: this.route })
  }
Run Code Online (Sandbox Code Playgroud)

路由器链接指令

这个指令最好的地方是它会ActivatedRoute为你检索。在引擎盖下它使用已经熟悉的:

router.navigateByUrl(router.createUrlTree(commands, { relativeTo: route }), { relativeTo: route });
Run Code Online (Sandbox Code Playgroud)

以下变体将产生相同的结果:

[routerLink]="['../..']"

// if the string parameter is passed it will be wrapped into an array
routerLink="../.."
Run Code Online (Sandbox Code Playgroud)


Spl*_*tar 5

除了提供的答案外,还有更多详细信息navigate。从函数的注释:

/**
 * Navigate based on the provided array of commands and a starting point.
 * If no starting route is provided, the navigation is absolute.
 *
 * Returns a promise that:
 * - resolves to 'true' when navigation succeeds,
 * - resolves to 'false' when navigation fails,
 * - is rejected when an error happens.
 *
 * ### Usage
 *
 * ```
 * router.navigate(['team', 33, 'user', 11], {relativeTo: route});
 *
 * // Navigate without updating the URL
 * router.navigate(['team', 33, 'user', 11], {relativeTo: route, skipLocationChange: true});
 * ```
 *
 * In opposite to `navigateByUrl`, `navigate` always takes a delta that is applied to the current
 * URL.
 */
Run Code Online (Sandbox Code Playgroud)

路由器指南》提供了有关程序化导航的更多详细信息。


Jyo*_*thi 5

根据我的理解,router.navigate 用于相对于当前路径进行导航。例如:如果我们当前的路径是abc.com/user,我们想要导航到 url:abc.com/user/10对于这种情况,我们可以使用 router.navigate 。


router.navigateByUrl() 用于绝对路径导航。

IE,

如果在这种情况下我们需要导航到完全不同的路线,我们可以使用 router.navigateByUrl

例如,如果我们需要从abc.com/user导航到abc.com/assets,在这种情况下我们可以使用 router.navigateByUrl()


句法 :

router.navigateByUrl(' ---- String ----');

router.navigate([], {relativeTo: route})