How to hide the parent component when loading a child component in Angular 2

拜拜、爱过 提交于 2021-01-27 04:40:29

问题


In my current project, I have the following view

When the page loads, the Parent Item Description should visible and the Selected sub item description should not visible.

When I select a Sub Item x, the Parent Item Description should be hidden and only the Selected sub item description visible.

I have created the following route for this

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

But when run the project, it doesn't work the way that I was expecting.

I have added a <router-outlet></router-outlet> to load the sub item description, but when I go to the main item, the main item itself replicated inside that router-outlet.

In summary, I wish to load only selected item's description in the right side.


回答1:


You need to convert the parent route to a componentless route like this:

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


来源:https://stackoverflow.com/questions/40732620/how-to-hide-the-parent-component-when-loading-a-child-component-in-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!