简体   繁体   English

如何在 app.component 的 Angular 中检测当前活动的父布局组件?

[英]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.

相关问题 如何将属性从一个组件绑定到角度为4的App.Component? - How to Bind Property from one component to App.Component in angular 4? 如何将 app.component 的数据存储在 angular 中? - How to store data from app.component in angular? Angular 2.如何检测组件当前是否在视口中 - Angular 2. How to detect if component is currently in viewport Angular Material sidenav指令在app.component中不起作用 - Angular Material sidenav directive does not work in app.component Angular不呈现HTML中来自app.component的值 - Angular not rendering value from app.component in HTML Angular Service将变量值传递给app.component - Angular Service passing variable value to app.component 如何在app.component外使用带有router-outlet的Angular 9导航栏 - How to use Angular 9 navigation bar with router-outlet outside the app.component 在网页ionic 2中使用app.component函数 - Use app.component function in a page ionic 2 当我点击 angular 中 app.component 中的导航栏时,到达我的路由器插座的 div - Reach the div of my router-outlet when I click on the navigation bar in the app.component in angular 如何在GeocoderService中存储来自geocoder.geocode()的地址并将其返回给app.component? - How to store address from geocoder.geocode() in GeocoderService and return it to app.component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM