如何在angular2中应用主题? [英] How to apply themes in angular2?

查看:90
本文介绍了如何在angular2中应用主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我在angular2中开发的Web应用程序提供两个主题(红色,蓝色).当我更改主题时,所有组件都应该反映出来吗?

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?

在angular2中应用主题的最佳实践是什么?

What are the best practices to apply themes in angular2?

推荐答案

您可以使用

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

这篇关于如何在angular2中应用主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆