我尝试使用带有外部链接的routerLink。这些链接在index.html上公开(就像来自外部应用程序的菜单一样),如下所示:
的index.html
<body>
<div>
<ul>
<li><a onclick="window.changePage('home')" routerLink="/">Home</a>
</li>
<li><a onclick="window.changePage('info')" routerLink="/">Info</a>
</li>
</ul>
</div>
<app-root></app-root>
</body>
当我从这个外部链接访问时,问题似乎是角度没有加载RouterModule。如果我通过this.router.navigate(....)导航,这可以正常工作。 我举了一个例子: plunker
步骤1:单击主页,然后单击转到信息按钮。你可以看到&#34;转到信息&#34;有一个routerLink,你可以点击它。 第2步:刷新页面。点击信息,您将看到&#34;转到信息&#34;无法点击它。 如何使用外部链接强制角度加载RouterModule?
答案 0 :(得分:1)
最好在.ts文件中对路由器进行路由。
在锚标记中,只需调用函数 window.changePage(&#39; home&#39;),类似地 window.changePage(&#39; info&#39;) ,和
在函数内的.ts文件中导航它。
答案 1 :(得分:0)
检查评论和其他答案,以下只是解决方法
@GünterZöchbauer已经在评论中提到了解决方案
更改您的信息组件,如下所示
import { Component } from '@angular/core';
@Component({
/* use href instead of routerlink*/
template: `Info HTML
<a href="/home">Go to Home</a>
`,
})
export class InfoComponent {
constructor() {
}
}