【发布时间】:2019-03-18 11:06:23
【问题描述】:
我有以下项目结构:GalleryModuleModule 有一个父组件GalleryComponent,其中有两个子组件:GalleryAddComponent 和GalleryItemComponent。当我想从组件GalleryComponent切换到组件GalleryAddComponent时,地址栏中的地址发生了变化,但是父组件并没有消失,也没有发生到子组件的转换。帮助了解问题是什么以及如何解决此问题。
GalleryRoutingModule:
const galleryRoutes: Routes = [
{
path: 'gallery',
component: GalleryComponent,
children: [
{path: 'gallery-add', component: GalleryAddComponent},
{path: 'galleryItem/:id', component: GalleryItemComponent},
]
}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(galleryRoutes)
],
exports: [RouterModule],
})
GalleryComponent 的模板:
<a routerLink="gallery-add" class="btn btn-outline-success tog">
Add New Post
</a>
<a [routerLink]="['./galleryItem', pic.id]">
<img [src]="pic.url" alt="test" class="img-responsive">
<p class="lead"><span>{{pic.id}}:</span>{{pic.title}}</p>
</a>
【问题讨论】:
-
你为什么用
'./galleryItem'? -
作为子路由,
GaleryAddComponent通常会添加到GalleryComponent内的router-outlet。如果要替换GalleryComponent,请将其添加到该children数组中,并使用空路径 -
当我使用“galleryItem”时没有任何变化
-
@user184994 thx,现在我看到了 add 和 item 组件,你能不能更详细地告诉我当我切换到“GalleryAddComponent”或“GalleryitemComponent”时如何隐藏“GalleryComponent”
-
@IgorShvets 你能在 StackBlitz 中创建一个基本的复制品吗?组件不需要功能,只需路由即可。然后我可以编辑它来告诉你我的意思
标签: javascript angular typescript routing angular-ui-router