How do I change the body class via a typescript class (angular2)

前端 未结 7 1846
失恋的感觉
失恋的感觉 2020-12-14 02:04

How do I change the body class via the root component?

@Component({ 
   selector: \"app\", 
   directives: [ROUTER_         


        
7条回答
  •  心在旅途
    2020-12-14 03:08

    One way that doesn't depend on direct DOM manipulation is, to make the tag the app element by using body as selector and use host-binding to update the app elements classes.

    @Component({
       selector: 'body',
       host:     {'[class.someClass]':'someField'}
    })
    export class AppElement implements AfterViewInit {
      someField: bool = false;
      // alternatively to the host parameter in `@Component`
      // @HostBinding('class.someClass') someField: bool = false;
    
      ngAfterViewInit() {
        someField = true; // set class `someClass` on ``
      }
    }
    

提交回复
热议问题