【发布时间】:2019-06-09 04:57:51
【问题描述】:
注意:请不要将其标记为重复,许多像我一样,正在等待答案。我们到处搜索。
1) 我有一个全局变量 $global_var_bg 在组件的 scss 文件中定义为 blue 颜色。
2) 我在组件的 scss 文件中有一个 mixin 函数,它接受 $theme 参数,该参数在应用程序的全局主题更改时传递。
3) 在 mixin 函数中我更改全局变量$global_var_bg。
4) 然后访问 scss class 中的全局变量 $global_var_bg。
5) 最后我将 class 分配 到 component.html 中的 div 元素,期望 $global_var_bg 成为 mixin 函数中修改后的 background_color。
6) 但是,它仍然是 $global_var_bg
帮我解决这个问题,
注意:我不想在 mixin 函数中移动类。
@import '~@angular/material/theming';
$global_var_bg: blue; //define a global variable
@mixin dashboard-component-theme($theme) {
$background: map-get($theme, background);
//modify the global variable inside the mixin function
$global_var_bg: mat-color($background, background) !global;
}
//access the global variable inside a class
.some-class {
background-color: $global_var_bg;
}
<!-- set the background color of the div -->
<!-- which I expect to be the theme's backround color -->
<!-- but still blue-->
<div class="some-class"> random text </div>
【问题讨论】:
-
你必须在定义类之前调用 mixin。您的代码仅定义 mixin。
-
我正在从 register_theme.scss 调用 mixin,当用户更改主题时会调用它......感谢@Jacob E 的回答,现在我有了这个想法。几天后,我会发布完整的答案。
-
无法通过应用交互动态调用 SCSS 文件或 SASS mixin。 SASS 是源代码,而不是实时 CSS 代码。您可以使用提供的答案来满足您的要求,但您在标题中提出的问题与 Jacob E 提供的对您的要求的解决方案无关。如果您完全更改标题,以便它反映应用程序要求问题,而不是次要的 SASS 变量问题。
标签: sass angular-material2 mixins