angular - 错误 : Cannot match any routes. URL 段: ''

标签 angular url-routing angular2-routing plunker angular2-router3

我正在尝试使用 Angular2 路由器重现我的问题,但我无法在 Plunker 中创建我的项目的工作副本。

这是我的尝试:https://plnkr.co/edit/1lpoYP4qlBZLuqZHW7Ft

我在 index.html 文件中使用了以下代码行,使路由路径与 Plunker 的运行环境和我的 '' 默认路径一起工作。

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

为什么我仍然收到此错误?

最佳答案

  1. 您的 plnkr 中有一个小错别字:

    loadChildren: 'app/拳头-section.module#FirstSectionModule'

    可能是第一个 ;)

  2. 还有一个问题:即使是 child 也需要一个空的''-path。所以不用写

children :[ { 路径:'第一节',loadChildren:'app/first-section.module#FirstSectionModule' } ] 您应该添加一个空路径并重定向到 first-section-path,如下所示:

children: [
    { path: '', redirectTo: 'first-section'},
    { path: 'first-section', loadChildren: 'app/fist-section.module#FirstSectionModule' }
]

这里是更正后的plnkr:

https://plnkr.co/edit/9fMwx0?p=preview


更新 Angular 2.4.1:

我注意到使用最新版本的 angular (2.4.1) 不再需要为子路由使用空路径。

我用最新的 angular 2.4.1 版本更新了 plunker:PLUNKER .该示例现在正在运行,没有空路径。

关于angular - 错误 : Cannot match any routes. URL 段: '',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40132186/

相关文章:

php - 登录后如何转发到正确的URL

ruby-on-rails - Rails 路由 : namespace nested in a scope

php - Yii框架中定义的Gii的路由在哪里?

angular - 从另一个模块访问路由参数

Angular2如何在保持路线的同时显示错误页面

javascript - 类型错误 : Cannot read property 'insertData' of undefined

asp.net - 如何在 aspx 中以 Angular 2 停止页面刷新

javascript - Html5 拖放,dragstart 后不触发 Mousewhell 事件

javascript - 如何使用 javascript、使用路由器和辅助 socket 重定向 Angular2 应用程序

javascript - 如何在 Angular 中使用第三方库动态创建组件?