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

前端 未结 7 1847
失恋的感觉
失恋的感觉 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

    Still looking for a better solution, here is my current workaround:

    import { Component, OnInit, OnDestroy, ViewEncapsulation } from '@angular/core';
    
    
    @Component({
        selector: 'signup',
        templateUrl: './signup.component.html',
        styleUrls: ['./signup.component.css',], // Where my custom CSS styles for body element declared
        encapsulation: ViewEncapsulation.None, // That will not encapsulate my CSS styles (layout-full, page-signup) from signup.component.css inside component
    })
    
    
    export class SignupComponent implements OnInit, OnDestroy{
    
        bodyClasses:string = "layout-full page-signup";
    
        ngOnInit(): void {
            $('body').addClass(this.bodyClasses);
        }
        ngOnDestroy() { 
            $('body').removeClass(this.bodyClasses);
        }
    
    
    }
    

提交回复
热议问题