【问题标题】:Angular2 nested routing incorrect URLAngular2嵌套路由不正确的URL
【发布时间】:2016-05-06 13:19:40
【问题描述】:

我正在开发一个 Angular2 应用程序,我注意到子路由不会从它们的父路由扩展。这是设计的吗?

例如,如果我有一个路由配置如下的父路由器:

// In top-level component

router.config([
   new AsyncRoute({
       path: '/designer/:id',
       name: 'Designer',
       loader: () => 
           System.import('app/components/design.component')
                 .then(c => c['DesignComponent'])
   }),
   new AsyncRoute({
       path: '/planner/:id',
       name: 'Planner',
       loader: () => 
           System.import('app/components/plan.component')
                 .then(c => c['PlanComponent'])
   })
]);

在这些组件中定义的子路由器和路由如下:

// In child component, design.component for example

router.config([
   new AsyncRoute({
       path: '/model',
       name: 'Model',
       loader: () => 
           System.import('app/design/components/model.component')
                 .then(c => c['ModelComponent'])
   })
]);

例如,当加载顶级页面时,我们可能会到达http://localhost:5000/designer/10,但是当导航到子路由时,URL 最终会变为http://localhost:5000/model 而不是http://localhost:5000/designer/10/model

预期:

http://localhost:5000/designer/10/model

实际:

http://localhost:5000/model

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    如果路由有子路由,则路径需要以/...结尾

    new AsyncRoute({
           path: '/designer/:id/...',
           name: 'Designer',
           loader: () => 
               System.import('app/components/design.component')
                     .then(c => c['DesignComponent'])
    

    这也可能是由异步路由引起的。只有加载子组件后才能解析完整的 URL。这在 Angular2 rc.0 中的新路由器中发生了变化

    【讨论】:

    • 我试过了,但它不起作用。事实上,它根本不导航。在不使用[routeLink] 的情况下设置父路由器是否重要?另外,RC.0 有什么变化?我将如何导航到以/... 结尾且以router.navigate 结尾的路线?
    • 你能创建一个允许复制的 Plunker 吗?
    • 我不明白您所说的“在不使用 [routeLink] 的情况下设置父路由器是否重要”。在 RC.0 中不再有路由名称。只有路径和组件。所有路由都是异步的 AFAIK(不确定)。当您导航时,您为每个级别传递一个元素['/Parent, {id: 'someId'}, 'Child']
    • 什么是通过window.location.href更改的URL,或者用户打开一个新的标签或窗口并输入URL? localhost:5000//design/10/model 什么都不做
    • 您的服务器上是否启用了 HTML5 pushState? stackoverflow.com/questions/31415052/…
    猜你喜欢
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 2016-08-21
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    相关资源
    最近更新 更多