javascript - 在 Angular 2 中加载子组件时如何隐藏父组件

标签 javascript angular angular2-routing

在我当前的项目中,我有以下 View

enter image description here

当页面加载时,Parent Item Description应该可见并且 Selected sub item description不应该可见。

当我选择 Sub Item x , Parent Item Description应该隐藏并且只有 Selected sub item description可见。

我为此创建了以下路线

{
        path: 'main/:id',
        component: MainComponent,
        children: [
          {
            path: '',
            component: MainComponent,
            pathMatch: 'full',
          },
          {
            path: 'sub/:id',
            component: SubComponent
          }
        ]
      },

但是当运行项目时,它并没有像我期望的那样工作。

我添加了一个 <router-outlet></router-outlet>加载子项目描述,但是当我转到主项目时,主项目本身在 router-outlet 中复制了.

总而言之,我希望只在右侧加载所选项目的描述。

最佳答案

您需要像这样将父路由转换为无组件路由:

 {
    path: 'main/:id',
    children: [
      {
        path: '',
        pathMatch: 'full'
        component: MainComponent,
      },
      {
        path: 'sub/:id',
        component: SubComponent
      }
    ]
  }

关于javascript - 在 Angular 2 中加载子组件时如何隐藏父组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40732620/

相关文章:

javascript - Angular 2动态更改默认路由

javascript - 将光标设置在空标签上

javascript - 通过名称获取元素 : doesn't work

Twitter 的 Javascript 包装器

angular - 在 Angular 中显示对象数组中的字段名称

angular - 在 angular2 中迁移到 RC4 后的问题

javascript - 上传图片时出现错误 413 有效载荷太大

css - 将 css 分配给 *ngFor item 的值

angular - i18n angular.json 的输出路径

angular - 括号在页面刷新时破坏 Angular 路由