【问题标题】:How do I stop Angular 2 router from appending the same route如何阻止 Angular 2 路由器附加相同的路由
【发布时间】:2016-09-29 10:22:16
【问题描述】:

当我刷新页面时,如何阻止 Angular2 路由器两次附加相同的路由?它可能不是角路由器。

转到它重定向联系人的应用的根目录,这很好。
localhost:3000/contact

刷新页面并附加相同的路由
localhost:3000/contact/contact

快速服务器仅包含 2 个路由处理程序。返回静态文件,其他一切返回 index.html 文件。

app.get(/\.\w{2,4}$/, function (req, res) {
  res.sendFile(path.join(__dirname, req.url));
});

app.get('*', function (req, res) {
  res.sendFile(path.join(__dirname, 'index.html'));
});

index.html 有基本的 href 集

<base href="/">

这是角度路线配置

export const routes: Routes = [
  {path: '', redirectTo: 'contact', pathMatch: 'full'}
]

【问题讨论】:

  • 我怀疑您问题中的代码会导致问题。您能否提供更多信息(其他路线,...)。你能在 Plunker 中重现吗? HashLocationStrategy 的表现如何?
  • 嘿 Günter,它无法在 Plunker 中重现,因为当我刷新页面时会发生问题,并且问题是我的环境的本地问题。如果有帮助,文件来自这个 plunk plnkr.co/edit/HEoqGVNga8s5tO4niNfg?p=preview 通过角度站点。我正在研究 ngModules 是如何工作的,因为它自 RC4 以来都发生了变化。所以我从这个 plunk 中取出文件并添加了一个非常简单的快速服务器。
  • 链接的 Plunker 不包含任何内容。
  • 我的错。请立即尝试:)
  • 我发现了问题,发布在下面。我看起来不够努力:)

标签: node.js express angular angular2-routing


【解决方案1】:

我在索引文件中发现了问题。这覆盖了我的基本设置...

<script>document.write('<base href="' + document.location + '" />');</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 2016-03-21
    相关资源
    最近更新 更多