[英]How to detect which parent layout component currently active in Angular in app.component?
How to check which layout component currently active in Angular in app.component?如何在 app.component 的 Angular 中检查当前活动的布局组件?
Here Is example my app-routing.module.ts.这是我的 app-routing.module.ts 示例。
{
path: '',
component: FrontComponent,
children: [
{
path: '',
redirectTo: 'login',
pathMatch: 'full'
},
{
path: 'login',
component: LoginComponent
}
]
},
{
path: '',
component: MainComponent,
canActivate: [AuthGuard],
children: [
{
path: 'dashboard',
component: DashboardComponent
},
{
path: 'message-list',
component: MessageListComponent,
}
]
}
Now I want to check layout component in app.component such as if it is ' FrontComponent ' or ' MainComponent '.现在我想检查 app.component 中的布局组件,例如它是“ FrontComponent ”还是“ MainComponent ”。 Because I want to perform some activity based on layout component.
因为我想基于布局组件执行一些活动。 I have searched other questions but could not get required answer.
我已经搜索了其他问题,但无法获得所需的答案。 can anyone please help me out?
谁能帮帮我? Thanks in advance.
提前致谢。 Specifically looking to check layout frontcomponent is active in app.component.ts
专门检查布局 frontcomponent 在 app.component.ts 中是否处于活动状态
You can make a service where you store the current layout component name string, and this variable gets overridden when switching to the other component.您可以创建一个存储当前布局组件名称字符串的服务,并且在切换到其他组件时会覆盖此变量。
You can learn it by looking at the URL with您可以通过查看 URL 来了解它
import { Component } from '@angular/core';
import { Router } from '@angular/router';
@Component({
template: ''
})
export class Component {
constructor(private router: Router) {}
ngOnInit() {
if ( this.router.url.indexOf('login') != -1) {
console.log('Father is FrontComponent');
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.