简体   繁体   English

当我使用路由器插座时,如何从父级制作子级路由中的空组件

[英]How can i make empty component in the child route,from the parent,when i use router outlet

I have a parent route where on click I show content from one child route and in the child route, on click, I fetch different data and I use router-outlet in the parent component's HTML. 我有一个父路由,单击该按钮时,我显示一个子路由的内容,而在子路由中,单击时,我获取不同的数据,并在父组件的HTML中使用router-outlet Now I want to empty the div which was rendered in the child route from the parent ts file based on a condition. 现在我想根据条件从父ts文件中清空在div渲染的div Any ideas? 有任何想法吗?

Here is my code 这是我的代码

// Here is the caller method
openRoleSection(roleId: string) {
    // Here is the condition, and when it is met, I need to empty the div in the parent route
    if (roleId != this.adminServices.serviceRoleId) {
        alert("yes")
    }
    // Here I am calling the parent routes
    this.router.navigateByUrl(`adminpanel/specific-role-section/${roleId}`, { relativeTo: this.activatedRoute })
}
<!-- I need to empty this div from the child route -->
<div *ngIf="isViewAllEmployeesButtonClicked">
    <div *ngFor="let employee of allUsersFromTheSameRole">
        <p>{{ employee }}</p>
    </div>
</div>

You can pass a query parameter to the child route on navigation, and use to populate/unpopulate specific information in the child component. 您可以在导航时将查询参数传递给子路径,并用于填充/取消填充子组件中的特定信息。

See https://angular.io/guide/router#query-parameters-and-fragments for more information 有关更多信息,请参见https://angular.io/guide/router#query-parameters-and-fragments

Eg something along the lines of 例如沿着

parent 父母

 const params:{
    roleId: this.adminServices.serviceRoleId
  }
 this.router.navigateByUrl(`adminpanel/specific-role-section/${roleId}` ,  {relativeTo: this.activatedRoute, , queryParams: params})

child 儿童

constructor(route:activatedRoute){
this.roleId= this.route
      .queryParamMap
      .pipe(map(params => params.get('roleId') || undefined));
}
<div *ngIf="roleId"></div>

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

相关问题 如何从子组件定位路由器出口? - How can I target router-outlet from a child component? Angular 8,当我在另一个路由器插座时,如何使用主插座路由 - Angular 8, how can I use the primary outlet route when i'm in another router outlet 来自儿童路线的Angular 4父路由器插座 - Angular 4 Parent Router Outlet from Child Route 如何从父组件调用路由器出口子组件方法 - How to call router outlet child component method from parent comonent 我可以使用父路由器插座来显示子级-&gt; 子级路由吗? - Can i use parent router-outlet for display child->child rout? 当子组件通过路由器出口调用时,如何从父组件向子组件传递数据? - How to pass data to a child component from parent component when the child component is being called through router-outlet? Angular 2:让子路由组件替换路由器插座中的Parent - Angular 2: Have Child Route Component Replace Parent in router-outlet 当子组件位于路由器出口Angular中时,从子组件调用父组件函数 - Call parent component function from child when child components are in router outlet Angular 如何以编程方式路由到命名路由器插座? - How can I route programmatically to a named router outlet? 如何从父路由的组件访问已激活的子路由的数据? - How can I access an activated child route's data from the parent route's component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM