Load css file dynamically for theming

前端 未结 1 1917
情歌与酒
情歌与酒 2021-01-05 01:24

I have three css files with different color themes e.g. theme1.css theme2.css theme3.css

I want to load them depending on the selected category. Is it possible to l

1条回答
  •  悲&欢浪女
    2021-01-05 02:01

    I am not sure if this is proper way. But you can try like this-

        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');
        }
    }
    

    Reference: https://angular.io/docs/ts/latest/api/platform-browser/index/DOCUMENT-let.html

    See if this helps.

    0 讨论(0)
提交回复
热议问题