# 在主题组件中使用变量结合@if的任何方法吗？

**URL:** <https://meta.discourse.org/t/is-there-any-way-to-use-if-in-a-theme-component-using-a-variable/350236>\
**Category:** Development\
**Created:** [2025年二月3日 13:01 UTC](https://meta.discourse.org/t/is-there-any-way-to-use-if-in-a-theme-component-using-a-variable/350236 "2025-02-03T13:01:45Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)\
**Post date:** [2025年二月3日 13:01 UTC](https://meta.discourse.org/t/is-there-any-way-to-use-if-in-a-theme-component-using-a-variable/350236/1 "2025-02-03T13:01:45Z")

</div>

我正在尝试使用布尔主题设置来确定我是否可以使用特定的样式设置：

```scss
@if var(--my-variable) {
   .my-selector {
      display: block;
  }
}

```

并在包含的文件中的其他位置：

```plaintext
:root {
  --my-variable: #{$my_boolean_setting_in_theme};
}

```

但这似乎不起作用……

无论变量是 true 还是 false，样式都会被应用。

---

_[View the full topic](https://meta.discourse.org/t/is-there-any-way-to-use-if-in-a-theme-component-using-a-variable/350236)._
