Angular 2 - How do I conditionally add styles to my component?

前端 未结 2 580
余生分开走
余生分开走 2021-01-14 13:56

I have a component with a stylesheet that loads correctly like this:

@Component({
  selector: \'open-account\',
  styleUrls: [\'open-account.component.scss\'         


        
2条回答
  •  感情败类
    2021-01-14 14:21

     import { Component, Inject } from '@angular/core';
     import { DOCUMENT } from '@angular/platform-browser';
    
     @Component({
     })
    
     export class SomeComponent {
    
        constructor (@Inject(DOCUMENT) private document) { }
    
            LightTheme() {
                this.document.getElementById('theme').setAttribute('href', 'light-theme.css');
    
    
            DarkTheme() {
                this.document.getElementById('theme').setAttribute('href', 'dark-theme.css');
        }
    }
    

提交回复
热议问题