簡體   English   中英

Angular:css變量在組件中不可用

[英]Angular: css variables are not usable in components

嗨,我正在嘗試使用 css 變量來預定義顏色。

這是我的 app.component.css

:root {
  --main-bg1: rgb(26, 28, 29);
  --main-bg2: rgb(66, 77, 79);
  --main-bg3: rgb(93, 109, 112);
}

問題是當我在其他組件中使用這個變量時它不會工作。 例如

.some-class{
  background-color: var(--main-bg1);
}

我怎樣才能使這項工作有角度?

移動這個:

:root {
  --main-bg1: rgb(26, 28, 29);
  --main-bg2: rgb(66, 77, 79);
  --main-bg3: rgb(93, 109, 112);
}

styles.css然后它會工作

演示

將這些樣式放入styles.css

:root {
  --main-bg1: rgb(26, 28, 29);
  --main-bg2: rgb(66, 77, 79);
  --main-bg3: rgb(93, 109, 112);
}

your.component.scss文件中將變量添加到:host范圍。

你的.component.html

:host {
  --drawer-width: 300px;
  --drawer-height: 65px;
  --drawer-top-height: 65px;
  --drawer-top: #ffffff;
  --drawer-left: red;
  --drawer-right: #f5f5f7;
  --drawer-top-bottom: red;

  /* Usage sample of variable */
  .use-sample-in-div {
    background: var(--drawer-left);
  }
}

您還可以使用主要的styles.scss更改值。 首先,將類名添加到您的組件中作為首要任務。

app.component.html

<Drawer class="drawer-class"></Drawer>

樣式.scss

:root .drawer-class {
  --drawer-width: 150px;
  --drawer-height: 32px;
  --drawer-top-height: 32px;
  --drawer-top: blue;
  --drawer-left: green;
  --drawer-right: purple;
  --drawer-top-bottom: pink;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM