首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Angular 6使用路由器出口

Angular 6使用路由器出口
EN

Stack Overflow用户
提问于 2018-07-28 11:01:14
回答 2查看 2.8K关注 0票数 1

我正在尝试了解命名插座在Augular 6中的工作原理。我希望能够将页面拆分为多个部分,并使该页面根据路由路径配置提供的多个命名插座动态显示不同的内容。我没有看到任何调试信息,也找不到问题所在。

示例:

app.component.html

代码语言:javascript
运行
复制
<!--<app-basic></app-basic> -->
<router-outlet></router-outlet>
<h1>hello</h1>
<router-outlet name="sidebar"></router-outlet>

路由器模块I具有以下功能:

代码语言:javascript
运行
复制
{
  path: 'schedule-inspection',
  component: ScheduleInspectionComponent,
  children: 
  [
    {
      path: 'add',
      component: SidebarScheduleInspectionComponent,
      outlet: 'sidebar',
    },
  ],
},

结果是页面正在加载父组件,并显示hello。没有内容显示在指定的插座之外。

你应该能够做到这一点似乎是合乎逻辑的。如果您在这里除了使用路由器之外还有其他方法,请告诉我,但它似乎是我正在尝试做的正确的方法。

EN

回答 2

Stack Overflow用户

发布于 2018-07-28 13:15:02

看起来您混合了子路由和辅助(已命名)路由。您不能使用children属性定义辅助路由。另请参阅此帖子:Child Routing for Angular 5 not working properly

所以就像这样:

代码语言:javascript
运行
复制
{
  path: 'schedule-inspection',
  component: ScheduleInspectionComponent
 },
 {
  path: 'add',
  component: SidebarScheduleInspectionComponent,
  outlet: 'sidebar',
 },
票数 0
EN

Stack Overflow用户

发布于 2018-07-28 17:58:29

我最终不得不执行以下操作,这并不是我所期望的。我希望/想,如果你能定义url的路径,让它知道如何填写其他路由器插座。

代码语言:javascript
运行
复制
Working:

{ path: 'schedule-inspection-sidebar', component: ServiceAreaComponent, outlet:'sidebar' },
{
  path: 'schedule-inspection',
  component: ScheduleInspectionComponent
},
{
    path: 'schedule-inspection(sidebar:schedule-inspection-sidebar)',
    component: SidebarScheduleInspectionComponent,
    outlet: 'sidebar'
},

然后,在浏览器中,转到schedule-inspection(sidebar:schedule-inspection-sidebar),它将查找名为schedule- content sidebar的路径,并显示主要内容。如果有更好的方法,请让我知道。

我希望能够转到/schedule-inspection,并且因为它有一个指定的路由器出口,所以它能够根据它的定义定位和显示该内容。最终用户永远不会直接转到计划-检查-侧边栏。

作为一种变通办法,我想我可以将逻辑构建到侧边栏组件中,直到显示哪个组件而不使用路由器插座。我在想,路由器可以管理该功能,而不是使用if语句。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51567658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档