错误:无法匹配任何路由.网址细分:''

sma*_*use 5 url-routing plunker angular2-routing angular2-router3 angular

我试图用Angular2路由器重现我的问题,但我不能在Plunker中创建我的项目的工作副本.

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

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

<script>document.write('<base href="' + document.location + '" />');</script>
Run Code Online (Sandbox Code Playgroud)

为什么我还在get这个错误?

Phi*_*ief 10

  1. 你的plnkr有一个小错字:

    loadChildren:'app/fist -section.module#FirstSectionModule'}

    可能是第一 ;)

  2. 还有一个问题:即使是孩子也需要一个空路''.所以不要写作

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

children: [
    { path: '', redirectTo: 'first-section'},
    { path: 'first-section', loadChildren: 'app/fist-section.module#FirstSectionModule' }
]
Run Code Online (Sandbox Code Playgroud)

这是纠正的plnkr:

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


更新Angular 2.4.1:

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

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

  • 问题是,你已经为你的'''-path定义了'AppComponent`.所以它会将孩子们加载到你的整个应用程序运行的根"<router-outlet>"中.您需要创建另一个组件,我将其命名为"StartComponent":请参阅更新的plunker:https://plnkr.co/edit/5f7Q0Pgji64l5GauDqMX?p = preview (2认同)