How to apply css class to a component element when it's created by router-outlet?

前端 未结 11 1437
南旧
南旧 2020-12-15 15:41

I have DOM that looks something like this:


    
    ...



        
相关标签:
11条回答
  • 2020-12-15 16:01

    Assuming you always want the class applied to this component, you can use host in your component metadata:

    @Component({
      selector:'project',
      host: {
          class:'classYouWantApplied'
      }
    })
    

    Resulting in:

    <app>
        <router-outlet></router-outlet>
        <project class="classYouWantApplied">...</project>
    </app>
    
    0 讨论(0)
  • 2020-12-15 16:02

    Currently, Angular 6 recommends me to use @HostBindings and @HostListeners instead of the host property:

    export class ProjectComponent {
      @HostBinding('class') classes = 'classYouWantApplied';
    }
    
    0 讨论(0)
  • 2020-12-15 16:07

    You can do this with a HostBinding, which is effectively the same as using the host property that has already been mentioned, although that method throws a TSLint error with default listing rules.

    In your component on which you want to apply a class:

    import { Component, HostBinding, Host (optional for typing) } from '@angular/core';
    
    @Component({...})
    export class GiveMeAClassComponent {
        @HostBinding('class.some-class') someClass: Host = true;
        ...
    }
    

    And then in your root styles.scss file, you can add the following:

    .some-class {
        // Styles in here will now be applied to your GiveMeAClassComponent at a root level
    }
    
    0 讨论(0)
  • 2020-12-15 16:09

    I created a RouterOutletHelperDirective which can be modified as necessary.

    Your use-case may be different but for me :

    • I needed to set a default set of classes on each router-outlet generated item
    • I needed to prevent this default based on certain conditions, such as ActivatedRoute data.

    You use it like this (the class is optional):

    <router-outlet routerOutletHelper
                   [routerOutletHelperClass]="'blue-border'"></router-outlet>
    

    Then create the directive, add and export it to your app module.

    import { Directive, ElementRef, Renderer2, Input } from "@angular/core";
    import { RouterOutlet } from "@angular/router";
    import { Subscription } from "rxjs";
    
    @Directive({
        selector: 'router-outlet[routerOutletHelper]'
    })
    export class RouterOutletHelperDirective
    {
        constructor(private routerOutlet: RouterOutlet,
                    private element: ElementRef<HTMLElement>,
                    private renderer: Renderer2) { }
    
        subscription = new Subscription();
    
        @Input('routerOutletHelperClass')
        customClassName: string | undefined;
    
        ngOnInit() 
        {
            this.subscription.add(this.routerOutlet.activateEvents.subscribe((_evt: any) => {
    
                // find the component element that was just added
                const componentElement = this.element.nativeElement.nextSibling;
    
                // add a custom class
                if (this.customClassName) 
                {
                    this.renderer.addClass(componentElement, this.customClassName);
                }
    
                // add my default classes, unless the activated route data 
                // (specified in module routing file) has { addDefaultClasses: false }
                if (this.routerOutlet.activatedRouteData && this.routerOutlet.activatedRouteData.addDefaultClasses !== false)
                {
                    // these are my application's default classes (material / theming)
                    // (an additional data parameter could be 'darkTheme: boolean')
                    this.renderer.addClass(componentElement, 'mat-typography');
                    this.renderer.addClass(componentElement, 'rr-theme-light');
                }
            }));
        }
    
        ngOnDestroy()
        {    
            this.subscription.unsubscribe();
        }
    }
    
    0 讨论(0)
  • 2020-12-15 16:09

    For me it helped to wrap the router-outlet into another container.

    <div class="classYouWantApplied">
      <router-outlet>
    </div>
    

    Like this you could apply the class to the surrounding container.

    0 讨论(0)
提交回复
热议问题