简体   繁体   English

如何覆盖 Angular 库 scss 变量?

[英]how to override Angular library scss variables?

I created a library in Angular 12 and I have implemented several components, I wanted to standardize the styling colors so I also implemented scss variables, I have created a SCSS file:我在 Angular 12 中创建了一个库,并实现了几个组件,我想标准化样式颜色,所以我还实现了 scss 变量,我创建了一个 SCSS 文件:

lib\dinotable.scss
$primary-color: green;
$secondary-color: red;

But I also want the library consumers to be able to override the colors in their consuming applications, how can I let them override the variables inside dinotable.scss?但我也希望图书馆消费者能够覆盖他们消费应用程序中的颜色,我怎样才能让他们覆盖 dinotable.scss 中的变量?

you can find the library here: https://github.com/chr1soscl/dinolib , use "npm run pack-dt" to build and pack the library, the consumer application is here: https://github.com/chr1soscl/dinolib-consumer你可以在这里找到库: https : //github.com/chr1soscl/dinolib ,使用“npm run pack-dt”来构建和打包库,消费者应用程序在这里: https : //github.com/chr1soscl/ dinolib-消费者

I would suggest trying to set the variables in the TS file instead, and let the end user override it in general configuration.我建议尝试在 TS 文件中设置变量,并让最终用户在一般配置中覆盖它。 Take a look at this: access SASS values ($colors from variables.scss) in Typescript (Angular2 ionic2)看看这个: 在 Typescript (Angular2 ionic2) 中访问 SASS 值 ($colors from variables.scss)

I would suggest to make a seperate SCSS file, maybe called overrides.scss, where the consumers can override the variables themselves.我建议制作一个单独的 SCSS 文件,可能称为 overrides.scss,消费者可以在其中自己覆盖变量。 However, you need to make sure that the override styles imported after the default Angular SCSS.但是,您需要确保在默认 Angular SCSS之后导入覆盖样式。 This means, you'll maybe have something like:这意味着,您可能会遇到以下情况:

lib\global-variables.scss

$primary-color: green;
$secondary-color: red;

@import 'overrides.scss'

Note: SASS are deprecating the @import feature and changes it to @use instead in the future.注意:SASS 正在弃用 @import 功能,并在将来将其更改为 @use。

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

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