【问题标题】:Error: Cannot match any routes. URL Segment: ''错误:无法匹配任何路由。网址段:''
【发布时间】:2017-03-01 03:22:36
【问题描述】:

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

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

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

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

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

【问题讨论】:

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


    【解决方案1】:
    1. 您的 plnkr 中有一个小错字:

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

      可能是第一个 ;)

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

    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' }
    ]
    

    这是更正后的 plnkr:

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


    更新 Angular 2.4.1:

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

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

    【讨论】:

    • 非常感谢您的支持!无论如何,您可以看到主应用程序组件加载了两次。你能再看看(更新的)Plunker 吗? plnkr.co/edit/AB6ac11Ne50Tj9zuzf57
    • 问题是,你已经为你的''-path 定义了AppComponent。因此,它会将孩子加载到您的根 &lt;router-outlet&gt; 中,您可以在其中运行整个应用程序。你需要创建另一个组件,我将它命名为StartComponent:查看更新的plunker:plnkr.co/edit/5f7Q0Pgji64l5GauDqMX?p=preview
    • 在不添加新的“容器”组件的情况下,我可以对路径进行哪些更改以使其正常工作?
    • 此外,当我点击“子页面”时,我想在“第一节容器”下看到它......但它在顶层加载(它取代了“第一节容器”的出现在它下面)。
    • 如果没有新的容器组件,您将无法使其正常工作。如果您为''-path 设置“AppComponent”,它将替换当前加载到父路由器出口中的当前组件。因此,您将整个 AppComponent 替换为例如“第一节比较”。试试看 :) 但是添加另一个组件会更好。如果您想查看“第一节容器”下的子页面,您必须再次创建子页面 (plunker)。
    猜你喜欢
    • 2018-09-01
    • 2017-08-10
    • 2017-11-13
    • 2020-04-14
    • 2020-01-23
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 2019-12-13
    相关资源
    最近更新 更多