如何在 Angular 2 中更改应用程序范围的 css? [英] How to change app wide css in Angular 2?

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

问题描述

在我的应用程序中,我有不同的 CSS 文件,如 fire.css、lake.css,每个文件都为完整的应用程序提供了不同的外观.

In my application I have different CSS file like fire.css, lake.css each giving different look to complete application.

目前,我仅使用 1 个文件 main.css 实现并将此文件添加到
index.html

Currently, I implemented with only 1 file main.css and added this file to
index.html

<link rel="stylesheet" href="resources/styles/main.css">  
<link rel="stylesheet" href="resources/styles/themes.css">
<link rel="stylesheet" href="resources/styles/common.css">
<link rel="stylesheet" href="resources/styles/plugins.css">

现在我想在用户从下拉列表中选择时动态更改此设置.

Now I wanted to change this dynamically as user select from the drop-down list.

我的想法:将所有 css 文件复制到 app 文件夹并在那里添加主题.
文件夹结构是

My Idea: Copy all css files to app folder and add themes there.
Folder structure is

app
|-----app.component.ts
|-----app.routes.ts
|-----main.css
|-----lake.css
|-----登录
|-----其他组件...

app
|-----app.component.ts
|-----app.routes.ts
|-----main.css
|-----lake.css
|-----Login
|-----Other Components...

我在 app.components.ts 中向 styleUrls 添加了 css 应用组件现在是

I added css to styleUrls in app.components.ts App component now is

import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';

@Component({

    selector: 'workshop-app',    
    template: `
        <body>
           <router-outlet></router-outlet>
       </body>
    `,
    directives : [ ROUTER_DIRECTIVES ],
    styleUrls : ['app/lake.css']
})
export class AppComponent { }

Lake.css 文件中的内容已添加到样式标记下,但不会在应用程序中进行更改.

Contents from Lake.css file is added to style tag under but not making changes in the app.

推荐答案

将此添加到 app.component.html

add this to the app.component.html

<head>
<link id="theme" rel='stylesheet' href='demo.css'>    
</head>  

将此添加到 app.component.ts

add this to app.component.ts

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

这篇关于如何在 Angular 2 中更改应用程序范围的 css?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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