与routerLink的相对链接/部分路由 [英] Relative links/partial routes with routerLink
问题描述
在我的应用中,某些URL的格式为
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屋!