简体   繁体   English

Angular router.navigate()delta

[英]Angular router.navigate() delta

As described in the manual, router.navigate accepts a delta, but the manual but isn't specific enough on that: 如手册中所述, router.navigate接受delta,但是手册但是不够具体:

Navigate based on the provided array of commands and a starting point. 根据提供的命令数组和起点进行导航。 If no starting route is provided, the navigation is absolute. 如果没有提供起始路线,则导航是绝对的。

... ...

In opposite to navigateByUrl, navigate always takes a delta that is applied to the current URL. 与navigateByUrl相反,navigation始终采用应用于当前URL的增量。

Does it just apply a relative URL or something more complex? 它只是应用相对URL还是更复杂的东西? What kind of delta does it refer to in case of absolute navigation then? 那么在绝对导航的情况下,它指的是什么样的三角洲呢?

Does it just apply a relative URL or something more complex? 它只是应用相对URL还是更复杂的东西?

It is a relative URL built from the pieces you provide via commands param and taking into account additional parameters you pass in extras (the NavigationExtras object ). 它是根据您通过commands参数提供的部分构建的相对URL,并考虑了您在extras传递的其他参数( NavigationExtras对象 )。

For example, you can use relativeTo to navigate from the active route or from the root route. 例如,您可以使用relativeTo从活动路由或根路由导航。 You can set the query parameters or fragment for the URL you navigate to ( queryParams and fragment in extras) or you can preserve query parameters that are present in the current url ( queryParamsHandling in extras). 您可以为导航到的URL设置查询参数或片段( queryParamsfragment在extras中),或者您可以保留当前URL中存在的查询参数( queryParamsHandling中的queryParamsHandling )。

And so on, so in general, it is actually something more complex than just a navigation by URL as we build the URL dynamically. 等等,所以一般来说,它实际上比我们动态构建URL时通过URL导航更复杂。

What kind of delta does it refer to in case of absolute navigation then? 那么在绝对导航的情况下,它指的是什么样的三角洲呢?

It is same for relative and absolute navigation - the delta is the set of changes ( commands ) to apply to the current route (relative) or to the root route (absolute) to transfer the application to the new state (vs just providing the new URL via the navigateByUrl ). 对于相对和绝对导航,它是相同的 - 增量是应用于当前路径(相对)的一组更改( commands )或应用于根路径(绝对)以将应用程序转移到新状态(仅提供新的URL通过navigateByUrl )。

In the simple case, if you do something like this.router.navigate(['/heroes']) it actually is not very different from using the navigateByUrl , but consider these examples (see the createUrlTree which actually converts commands and extras to the final URL): 在简单的情况下,如果您执行类似this.router.navigate(['/heroes'])它实际上与使用navigateByUrl没有什么不同,但请考虑这些示例(请参阅createUrlTree ,它实际上将commandsextras转换为最终网址):

// create /team/33/(user/11//right:chat)
router.createUrlTree(['/team', 33, {outlets: {primary: 'user/11', right: 'chat'}}]);

// remove the right secondary node
router.createUrlTree(['/team', 33, {outlets: {primary: 'user/11', right: null}}]);

So even for the absolute navigation, the navigate method provides a set of additional tools to dynamically build the URL. 因此,即使对于绝对导航, navigate方法也提供了一组动态构建URL的附加工具。 You could do this with navigateByUrl , but you would probably parse / concat / do other manipluations with strings (or would develop own tool similar to what navigate and createUrlTree provide). 您可以使用navigateByUrl执行此操作,但您可能会使用字符串解析/连接/执行其他操作(或者将开发类似于navigatecreateUrlTree提供的自己的工具)。

in relative mode, the delta is applied to the current route. 在相对模式下,delta应用于当前路线。 It natively calls navigateByUrl using this code 它使用此代码本机调用navigateByUrl

/**
   * 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.
   */
  navigate(commands: any[], extras: NavigationExtras = {skipLocationChange: false}):
      Promise<boolean> {
    validateCommands(commands);
    return this.navigateByUrl(this.createUrlTree(commands, extras), extras);
  }

many examples are included in the source code https://github.com/angular/angular/blob/master/packages/router/src/router.ts 源代码中包含了许多示例https://github.com/angular/angular/blob/master/packages/router/src/router.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM