[英]How to hide a parent component when switching to a child component
When I start the application, I have the "AppComponent" component displayed. 启动应用程序时,将显示“ AppComponent”组件。 Through it, using
AuthGuard
I turn or do not go to the module routes: GalleryModule
. 通过它,我使用
AuthGuard
转到或不转到模块路由: GalleryModule
。 My problem is that when I go over the child routes GalleryAddComponent
or GalleryItemComponent
to GalleryComponent
, my father GalleryComponent
does not hide, and the child component is displayed below. 我的问题是,当我越过子路线将
GalleryAddComponent
或GalleryItemComponent
路由到GalleryComponent
,我的父亲GalleryComponent
不会隐藏,并且在下面显示了子组件。 and I need that during the transition on the child GalleryAddComponent
and GalleryItemComponent
my father would hide GalleryComponent
. 我需要在子项
GalleryAddComponent
和GalleryItemComponent
过渡期间,我父亲要隐藏GalleryComponent
。 How to implement it? 如何执行呢?
Structure of my project: 我的项目结构:
AppRoutingModule: AppRoutingModule:
const appRoutes: Routes = [ { path: '', pathMatch: 'full', redirectTo: 'app-root' }, ]; @NgModule({ imports: [ CommonModule, RouterModule.forRoot(appRoutes) ], exports: [RouterModule], })
GalleryRoutingModule: GalleryRoutingModule:
const galleryRoutes: Routes = [ { path: 'gallery', canActivate: [AuthGuard], component: GalleryComponent, children: [ {path: 'gallery-add', component: GalleryAddComponent}, {path: 'galleryItem/:id', component: GalleryItemComponent}, ] }, ]; @NgModule({ imports: [ CommonModule, RouterModule.forChild(galleryRoutes) ], exports: [RouterModule], declarations: [] })
template of GalleryComponent: GalleryComponent模板:
<div class="header"> <div class="row"> <div class="col-lg-12 col-md-12 col-12"> <h1>{{gallery}}</h1> <div class="create-post"> <a routerLink="gallery-add" class="btn btn-outline-success tog" > Add New Post </a> </div> </div> </div> </div> <div class="row"> <div class="col-lg-3 col-md-4 col-6" *ngFor="let pic of collection"> <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> <div class="card buttons"> <a class="btn btn-danger del" (click)="removePost(pic.id)">Delete</a> </div> </div> </div> <div class="row"> <div class="col-lg-12 col-md-12 col-12"> </div> </div> <router-outlet></router-outlet>
SO you have to remove the child routes from GalleryRoutingModule . 因此,您必须从GalleryRoutingModule删除子路由。 And router-outlet from the Gallerycomponent.
还有来自Gallery组件的路由器插座 。 And need to make the routes normal way (no nested/ child routes ) in the GalleryRoutingModule
并且需要在GalleryRoutingModule中使路由正常(无嵌套/子路由)
The problem is with the placement of <router-outlet></router-outlet>
. 问题在于
<router-outlet></router-outlet>
。 It should not be in gallery.component.html
. 它不应在
gallery.component.html
。 If you have <router-outlet></router-outlet>
in app.component.html then it will handle the routing. 如果app.component.html中有
<router-outlet></router-outlet>
,则它将处理路由。
In short just remove <router-outlet></router-outlet>
简而言之,只需删除
<router-outlet></router-outlet>
If you are facing the issue related AuthGuard. 如果您遇到与AuthGuard相关的问题。 Create new Component which handles the routing for the Gallery.
创建新的组件来处理库的路由。
- Create Main Component for Gallary
创建Gallary的主要组件
<router-outlet></router-outlet>
@Component
Class GalleryMainComponent{
}
- Change in the routing-
更改路由-
const galleryRoutes: Routes = [
{
path: '',
canActivate: [AuthGuard],
component: GalleryMainComponent,
children: [
{path: 'gallery', component: GalleryComponent},
{path: 'gallery-add', component: GalleryAddComponent},
{path: 'galleryItem/:id', component: GalleryItemComponent},
]
},
];
Note : Do not forget to remove the <router-outlet></router-outlet>
from gallery.component.html
注意:不要忘记从
gallery.component.html
删除<router-outlet></router-outlet>
Service: 服务:
public navigation = new Subject<any>();
public navigation$ = this.navigation.asObservable();
In GalleryAddComponent & GalleryItemComponent, You can create emit an observable using service. 在GalleryAddComponent和GalleryItemComponent中,您可以创建一个可观察的使用服务发射。
this.service.navigation.next(true);
In Gallery Component, you can subscribe to listen for the changes. 在图库组件中,您可以订阅以侦听更改。
public displayGalleryContent = true;
this.service.navigation$.subscribe((isreached)=>{
if(isreached){
this.displayGalleryContent = true;
}
});
Template: 模板:
<div *ngIf="displayGalleryContent"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.