angular - 如何检查来自父路由器或父路由器的事件子路由器?

标签 angular if-statement conditional-statements router angular-routing

如何检查子路由器是否处于事件状态,以 Angular 4 显示状态 true 或 false,
现在我正在使用: /* @ Angular/cli:1.4.4 节点:8.6.0 typescript :2.3.4 @ Angular/路由器: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';

和生命周期钩子(Hook) ngOnInit() 并创建 ActivatedRoute 的 Refarence

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

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

关于angular - 如何检查来自父路由器或父路由器的事件子路由器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46722761/

相关文章:

javascript - 在条件语句中递增变量

ruby-on-rails - 是否可以在 has_many 关系上使用 count 条件?

r - 如何根据一个数据帧中的列的值和R中另一个数据帧的列标题名称有条件地创建新列

Angular 4 下载 base64 图像数组作为 ZIP 文件

angular - FormBuilder 中的嵌套属性

java - 用 Java 模拟骰子游戏程序

javascript - if 语句返回未定义

html - 动态生成具有angular 2的输入字段类型并设置字段的类型

angular - 无法解析 [...] 的所有参数

jquery - 试图在每个语句中获取 this 的父级