简体   繁体   English

切换到子组件时如何隐藏父组件

[英]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. 我的问题是,当我越过子路线将GalleryAddComponentGalleryItemComponent路由到GalleryComponent ,我的父亲GalleryComponent不会隐藏,并且在下面显示了子组件。 and I need that during the transition on the child GalleryAddComponent and GalleryItemComponent my father would hide GalleryComponent . 我需要在子项GalleryAddComponentGalleryItemComponent过渡期间,我父亲要隐藏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>

Alternative 另类

If you are facing the issue related AuthGuard. 如果您遇到与AuthGuard相关的问题。 Create new Component which handles the routing for the Gallery. 创建新的组件来处理库的路由。

Follow these steps 跟着这些步骤

  1. Create Main Component for Gallary 创建Gallary的主要组件

gallery-main.component.html gallery-main.component.html

<router-outlet></router-outlet>

gallery-main.component.ts gallery-main.component.ts

@Component
Class GalleryMainComponent{

}
  1. 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.

相关问题 在Angular 2中加载子组件时如何隐藏父组件 - How to hide the parent component when loading a child component in Angular 2 使用 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 <button>在子组件中</button>隐藏父组件<button>元素</button> - Hide parent component <button> element in Child component React中子组件更新时如何更新父组件 - How to Update the parent component when child component updates in React 在父组件中增加子组件时如何更新子组件? - How to update a child component when incrementing it in the parent component? 当子组件中发生onclick事件时,如何重新呈现父组件? - how to rerender parent component when onclick event happend in child component? 如何让父组件知道所有子组件何时加载 - How to let a parent component know when all the child component are loaded 更改子组件时如何更新父组件? - how to update parent component when changing child component? 切换视图时如何避免子组件的额外或不必要的渲染? - How to avoid extra or unnecessary renders of a child component when switching views?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM