Angular 如何从父路由器或父路由器检查活动子路由器?
如何可能检查子路由器是否处于活动状态,在angular 4中显示状态true或false,Angular 如何从父路由器或父路由器检查活动子路由器?,angular,if-statement,conditional-statements,router,angular-routing,Angular,If Statement,Conditional Statements,Router,Angular Routing,如何可能检查子路由器是否处于活动状态,在angular 4中显示状态true或false, 现在我正在使用: /* @角度/cli:1.4.4 节点:8.6.0 打字稿:2.3.4 @角度/路由器:4.4.4 */ 我的父母路线是: const routes:Routes=[ { path: '', component: SummaryOfFindingsComponent, children:[ {
现在我正在使用: /* @角度/cli:1.4.4 节点:8.6.0 打字稿:2.3.4 @角度/路由器: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()并创建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
}
}
注意:这段代码正在我的应用程序中运行(谢谢)这里有一种方法可以在可观察的环境中观察儿童
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))
这只在第一次加载父组件时起作用,但如果您想跟踪更改,我需要将此页面中的任何路由器更改订阅到ngOnInit()中的activeRouter.params。