简体   繁体   English

有条件地将 CSS 文件导入到 Angular 组件中

[英]Conditionally import CSS files in to Angular component

I have different CSS files(red.css,blue.css) in the single Angular Component.How can we import the css file according to the condition.我在单个 Angular 组件中有不同的 CSS 文件(red.css,blue.css)。我们如何根据条件导入 css 文件。 If I mentioned the color as red the red.css file should be imported.如果我提到颜色为红色,则应该导入 red.css 文件。

 this.activatedRoute.paramMap.subscribe(params=> { let color=params.get('color'); console.log(color); })
 red.css .my-container{ height: 100%; background-color: red; } blue.css .my-container{ height: 100%; background-color: blue; }
 <div class="my-container"> <h1 class="sideheading">URL that entered are processed for Theme Changing of the Page</h1> </div>

Multiple CSS Files and condition that takes color as input and should apply the css file according to the given color.多个 CSS 文件和条件,将颜色作为输入,并应根据给定的颜色应用 css 文件。

CSS part: CSS部分:

.my-red-container{
    height: 100%;
    background-color: red;
}

.my-blue-container{
    height: 100%;
    background-color: blue;
}

Angular part: let's suppose that You have typescript var named color and Its value is 'blue' or 'red角度部分:假设您有名为 color 的 typescript var,其值为“blue”或“red”

  <div
    [ngClass]="{'my-red-container': color === 'red', 'my-blue-container' : color === 'blue' }"
    ></div>

CSS Part CSS 部分

.my-red-container{
    height: 100%;
    background-color: red;
}

.my-blue-container{
    height: 100%;
    background-color: blue;
}

Component.html Part Component.html 部分

<div [ngClass]="{color === 'red' ? 'my-red-container' : 'my-blue-container'}">
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM