简体   繁体   English

角头相关的routerLink

[英]Angular header relative routerLink

I am trying to make a global header in Angular6, with a dynamic list of hyperlinks, which are different for each page. 我试图在Angular6中创建一个全局标题,其中包含动态链接的超链接,每个页面的超链接都不同。 The hyperlinks always reference some page, relative to the current URL. 超链接始终引用相对于当前URL的某些页面。 But the routerLink-s do not work relatively in the global header. 但是routerLink-s在全局头中不能相对工作。 This means that, when I have a routerlink in the app root header and I try to make relative links to the current, lazy loaded module, which is currently in use and which I have loaded a menu for. 这意味着,当我在应用程序根标题中具有routerlink时,我尝试建立到当前延迟加载的模块的相对链接,该模块当前正在使用中,并且已为其加载菜单。

Meaning, if I am on an URL 意思是,如果我在URL上

http://example.com/company/8/employee

And the global menu holds a hyperlink 全局菜单包含一个超链接

<a routerLink="./profile">

Then clicking on that routerLink, will take me to example.com/profile. 然后点击该routerLink,将我带到example.com/profile。 It acts as an absolute link. 它充当绝对链接。 Now if, on the same page, I insert the same code into the employee component, which is loaded as the main body, it works differently, in it that it will redirect me to. 现在,如果我在同一页面上将相同的代码插入到作为主体加载的员工组件中,那么它的工作方式将有所不同,因为它将重定向我。

http://example.com/company/8/employee/profile

I do not understand, why wouldn't it take the global router url state as the relativity base, but instead it tries to route from root, where the header is - in the router tree. 我不明白,为什么不将全局路由器url状态作为相对性基础,而是尝试在路由器树中从标头为root的根进行路由。

How can I possibly manage global relative menus in the header in Angular, when the relative URL-s do not work correctly there? 当相对URL在此处无法正常工作时,如何管理Angular标头中的全局相对菜单?

Since ./ is a prefix of the routerLink , it will consider the current URL as the base for its redirection. 由于./routerLink的前缀,因此它将当前URL作为其重定向的基础。

./ is nothing but a relative path, so if you put <a routerLink="./profile"> in the app.component , it will redirect to example.com/profile and if you put the same link in the employee.component , it will redirect to http://example.com/company/8/employee/profile ./只是相对路径,因此,如果将<a routerLink="./profile">放在app.component ,它将重定向到example.com/profile并且如果您将相同的链接放在employee.component ,它将重定向到http://example.com/company/8/employee/profile

If you need absolute path redirection, just add / (ex: <a routerLink="/profile"> ) so that irrespective of the component, it will get redirect to example.com/profile 如果您需要绝对路径重定向,只需添加/ (例如: <a routerLink="/profile"> ),以便无论使用<a routerLink="/profile">组件,它都将重定向到example.com/profile

I found an answer a few weeks later. 几周后我找到了答案。

You are allowed to pass in relativeTo to the router, which helps in these cases. 允许您将relativeTo传递到路由器,这在这些情况下会有所帮助。 You can forward another components ActivatedRoute and use that as a relativity base. 您可以转发其他组件ActivatedRoute并将其用作相对论基础。

this._router.navigate(['../../method'], {relativeTo: this._activatedRoute});

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

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