【发布时间】:2018-11-17 16:52:27
【问题描述】:
想象一下 Angular 组件的 my.component.scss 文件中有一个 SCSS mixin。
现在我在 Angular 应用程序的全局 style.scss 中导入该文件,因为我想将一些数据传递给该 mixin。
例如: my-component.scss.
<pre>
@mixin my-component-theming($theme) {
// Extract whichever individual palettes you need from the theme.
$warn-palette: map-get($theme, warn);
$warn: mat-color($primary-palette);
.error-message {
color: $warn !important;
}
}
</pre>
my-component.ts
<pre>
@Component({
selector: 'my-component',
templateUrl: './my-component.html',
styleUrls: ['./my-component.scss']
})
</pre>
Angular 应用的styles.scss。 (这会在 Angular.json 中导入)
<pre>
@import '~@angular/material/theming';
@import 'my-component.scss';
// just imagine that $app-theme is already defined here.
// Not including unnecessary code to keep example simple.
@include my-component-theming($app-theme)
</pre>
它确实按预期编译和应用程序工作,但我的“错误消息”css 类可供整个应用程序访问。
Angular 应该已经封装了“error-message”css 类,并且它的可见性应该仅限于 my-component。在这种情况下如何保持封装工作?
PS:我只是想完成这个:https://material.angular.io/guide/theming#theming-only-certain-components 但这个问题似乎更普遍。
提前致谢。如果需要进一步说明或代码,请告诉我。
【问题讨论】:
标签: angular sass angular-material angular6 angular-material-theming