简体   繁体   English

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

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

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.当页面加载时, Parent Item Description应该可见,而Selected sub item description应该不可见。

When I select a Sub Item x , the Parent Item Description should be hidden and only the Selected sub item description visible.当我选择一个Sub Item xParent Item Description应该被隐藏,只有Selected sub item description可见。

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 .我添加了一个<router-outlet></router-outlet>来加载子项描述,但是当我转到主项时,主项本身复制到该router-outlet

In summary, I wish to load only selected item's description in the right side.总之,我希望仅在右侧加载所选项目的描述。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 切换到子组件时如何隐藏父组件 - How to hide a parent component when switching to a child component 使用 map 按钮时,如何通过子组件中的按钮显示/隐藏父组件中的组件? - How to show/hide component in parent component via a button in a child component when using a map of buttons? REACT:当子组件有错误时隐藏父组件 - REACT : hide parent component when child component has an error 当子组件加载时,在父组件中加载React侧边栏 - Loading React Sidebar in Parent Component when Child Component Loads <button>在子组件中</button>隐藏父组件<button>元素</button> - Hide parent component <button> element in Child component angular:7如何在父组件上测试子发射器? - angular : 7 How test child emitter on parent component? 在Angular的“子组件”中单击按钮时,如何为父组件属性设置值 - How to set value to parent component property when a button is clicked in Child component in Angular 加载父组件数据后,如何从子组件调用函数(Angular 5) - How to call function from child component when parent component data has loaded (Angular 5) 父组件如何在Angular中将HTML传递给子组件? - How parent component pass HTML to child component in Angular? 如何从 Angular 9 中的子组件访问父组件 - How to Access Parent Component from Child Component in Angular 9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM