Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
主要组件在实现延迟加载模块后在路由器出口渲染两次-Angular 8_Angular_Components_Angular Routing_Router Outlet - Fatal编程技术网

主要组件在实现延迟加载模块后在路由器出口渲染两次-Angular 8

主要组件在实现延迟加载模块后在路由器出口渲染两次-Angular 8,angular,components,angular-routing,router-outlet,Angular,Components,Angular Routing,Router Outlet,以下是预期的用户界面: 但实际的UI是渲染的 这是app-routing.module.ts const routes: Routes=[ {path: '',component: HomeComponent}, {path:'counter', loadChildren:() => import('./counter/counter.module').then((m)=> m.CounterModule) }, {path:'posts', component: Postsli

以下是预期的用户界面:

但实际的UI是渲染的

这是app-routing.module.ts

const routes: Routes=[
{path: '',component: HomeComponent},
{path:'counter',
loadChildren:() => import('./counter/counter.module').then((m)=> m.CounterModule)
},
{path:'posts', component: PostslistComponent,

loadChildren:() => import('./posts/posts.module').then((m)=>m.PostsModule),

}
];
const routes:Routes=[
{path:'', component: PostslistComponent,
children:[
{path:'update/:id', component:UpdatepostComponent,
   
},
{path:'add',component:AddpostComponent, 
},
  
]
}
];
========================================================

Post.module.ts

const routes: Routes=[
{path: '',component: HomeComponent},
{path:'counter',
loadChildren:() => import('./counter/counter.module').then((m)=> m.CounterModule)
},
{path:'posts', component: PostslistComponent,

loadChildren:() => import('./posts/posts.module').then((m)=>m.PostsModule),

}
];
const routes:Routes=[
{path:'', component: PostslistComponent,
children:[
{path:'update/:id', component:UpdatepostComponent,
   
},
{path:'add',component:AddpostComponent, 
},
  
]
}
];
===================================================== app.component.html(具有主路由器出口)


========================================================= postslist.component.html 这里我有另一个路由器出口来添加/更新/删除帖子

<div class="col-md-4">
<router-outlet></router-outlet>
     
</div>

问题是,在上面的路由器出口中,相同的组件再次被渲染,
有人能帮我解决吗?

这是因为您在父路由中导入了
PostListComponent
。只需删除它,以及从父模块
@ngModule
装饰器中删除模块后导入

一定是这样

{
  path:'posts',
  loadChildren:() => import('./posts/posts.module').then((m)=>m.PostsModule),
}

只要代码正常工作,就意味着将PostModule导入父模块,必须将其删除。

使用命名路由器出口,这有助于区分多个路由器出口。 有关详细示例,请参阅下面的链接


非常感谢,它现在可以正常工作了。谢谢你的帮助!不客气:)