繁体   English   中英

如何从父路由器或父路由器检查活动子路由器?

[英]how to check active child router from parent router or parent router against?

如何检查子路由器是否活动,在角度 4 中显示状态真或假,
现在我正在使用:/* @angular/cli: 1.4.4 node: 8.6.0 typescript: 2.3.4 @angular/router: 4.4.4 */

我的父母路线是:

const routes:Routes=[
    {
        path: '', component: SummaryOfFindingsComponent,
        children:[
            {
                path:'data-time-frame', component: DataTimeFrameComponent
            },
            {
                path:'email-address', component: EmailAddressesComponent
            },
            {
                path:'repair-orders', component: RepairOrdersComponent
            },
            {
                path:'total-records', component:TotalRecordsComponent
            },
            {
                path:'unique-households', component: UniqueHouseholdsComponent
            },
            {
                path:'unique-vins', component: UniqueVinsComponent
            }
        ]
    }
]

父组件是:

export class SummaryOfFindingsComponent implements OnInit {
    isUserSelected;
    constructor() { this.isUserSelected=false; }
    ngOnInit() { }
    isUserItemSelect(){
        this.isUserSelected=true;
    }
}

导入你的父组件.ts

import { ActivatedRoute } from '@angular/router';

和生命周期挂钩 ngOnInit() 和创建 Refarence 的 ActivatedRoute

constructor(private activeRouter:ActivatedRoute) {}

并在您的 ngOnInit 函数中编写一些代码。

ngOnInit() {
    var _activeChild = this.activeRouter.children.length;
    if (_activeChild!=0) {
        //your active children 1 or more than children then active 1,otherwise it is 0
    }
}

注意:此代码正在运行我的应用程序(谢谢)

这是一种在 Observable 中观察孩子的方法......

this.router.events.pipe(
  filter(event => event instanceof ActivationEnd),
  filter(event => (event as ActivationEnd).snapshot.component === SearchFeatureComponent),
  map(event => (event as ActivationEnd).snapshot.children.length),
  distinctUntilChanged()
).subscribe(children => console.warn('route children', children))

我需要一个简单的 Observable 解决方案,它会发出false的没有子路由处于活动状态。

一起去:


  constructor(
    private router: Router,
    private route: ActivatedRoute
  ) {}

  readonly childActive$ = this.router.events.pipe(
    switchMap(() => of(this.route.children.length > 0))
  );

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM