How to apply themes in angular2?

前端 未结 2 900
予麋鹿
予麋鹿 2021-01-21 14:01

I need to provide two themes(red, blue) for the web application that I am developing in angular2. When I change the theme, all the components should reflect it?

What are

2条回答
  •  囚心锁ツ
    2021-01-21 14:45

    You can use the DOCUMENT token from @angular/platform-browser to gain access to all DOM element and then change the stylesheet source. Below is a simple example.

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

提交回复
热议问题