简体   繁体   English

从子组件中移除父组件

[英]Remove parent component from child

I am experiencing trouble with routing in angular. My URLs seem to work fine but my problem is that when I visit the child component(CreateEventsComponent) the parent component(EventsComponent) shows as well.我在 angular 中遇到路由问题。我的 URL 似乎工作正常,但我的问题是当我访问子组件 (CreateEventsComponent) 时,父组件 (EventsComponent) 也会显示。

How can I remove it from my component?如何从我的组件中删除它?

const routes: Routes = [
      {
        path: '',
        component: EventsComponent,
        data: {
          title: 'Events',
        },
        children: [
          {
            path: 'create',
            component: CreateEventsComponent,
            data: {
              title: 'Create Events',
            },
          },
        ],
      },
    ];

This is my parent html code这是我的父母 html 代码

<p>Events works!</p>
<router-outlet></router-outlet>

This is my child html code这是我的孩子 html 代码

<p> Create Events works </p>

The code is working as intended if you dont want the parent component to display you have to change the design to something like this如果您不想显示父组件,则代码按预期工作,您必须将设计更改为这样的东西

  {
        path: '',
        component: EventsComponent,
        data: {
          title: 'Events',
        },
  },
  {
           path: 'create',
            component: CreateEventsComponent,
            data: {
              title: 'Create Events',
            },
  },]

This ensures that when you visit /create it will only load the CreateEventComponent.这确保当您访问/create时它只会加载 CreateEventComponent。 Currently it is loading the EventsComponent with the CreateEventsComponent as a child component.当前,它正在加载 EventsComponent,并将 CreateEventsComponent 作为子组件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM