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

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

I have DOM that looks something like this:


    
    ...

         


        
11条回答
  •  悲&欢浪女
    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
    }
    

提交回复
热议问题