使用 routerLink 的相对链接/部分路由 [英] Relative links/partial routes with routerLink
问题描述
在我的应用中,一些网址采用了这种形式
In my app some URLs take the form
/department/:dep/employee/:emp/contacts
在我的侧边栏中,我显示了所有员工的列表,每个员工都有一个 [routerLink]
链接到该员工的联系人
In my sidebar I show a list of all employees, each of which has a [routerLink]
which links to that employee's contacts
<ul>
<li>
<a [routerLink]="['/department', 1, 'employee', 1, 'contacts']"></a>
<li>
<li>
<a [routerLink]="['/department', 1, 'employee', 2, 'contacts']"></a>
<li>
...
</ul>
然而,使用这种方法,我总是需要提供完整路径,包括所有参数(如上例中的 dep
),这非常麻烦.有没有办法只提供路线的一部分,例如
However, with this approach I always need to provide the full path, including all the params (like dep
in the above example) which is quite cumbersome. Is there a way to provide just part of the route, such as
<a [routerLink]="['employee', 2, 'contacts']"></a>
(没有 department
部分,因为在那个视图中我并不真正关心 department
并且我的感觉是这无论如何都不利于关注点分离)?
(without the department
part, because I don't really care about department
in that view and my feeling is that this goes against separation of concerns anyway)?
推荐答案
这应该可行.前导 /
使其成为绝对路由,没有它(或带有 ./
),它就成为相对于当前路由的相对路由.您还可以使用 ../
(或 ../../
或更多)相对于父级或父级父级进行路由.
That's supposed to work. The leading /
makes it an absolute route, without it (or with ./
), it becomes a relative route relative to the current route. You can also use ../
(or ../../
or more) to route relative to the parent or parents parent.
这篇关于使用 routerLink 的相对链接/部分路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!