简体   繁体   English

如何在Angular组件的CSS中使用第三方CSS库而不重复库?

[英]How can I use a third party CSS library in an Angular component's CSS without duplicating the library?

I have a two components that both rely on effects from hover.css . 我有两个组件都依赖于hover.css效果。 Both components have SASS files that (simplified) look something like this: 这两个组件都有SASS文件(简化)看起来像这样:

@import '~hover.css/scss/hover';

.some-class a {
  @include underline-from-left;
}

Additionally, I have the hover.css library included in my global styles in style.css : 另外,我在style.css全局样式中包含了hover.css库:

@import '~hover.css/scss/hover';

.some-global-class {
  @include some-other-mixin-from-hover;
}

This all works and compiles fine, except for the rather large fly in the ointment that I end up with full hover.css in my compiled application three times - once in styles.js and twice in main.js (once for each component). 这一切都可以正常编译,除了软膏中相当大的飞行,我在我的编译应用程序中完成了三次完整的hover.css - 一次在styles.js ,两次在main.js (每个组件一次)。 This is obviously not a sustainable pattern. 这显然不是一种可持续的模式。

If I don't @import hover.css in my components though, Angular won't compile them because they reference a mixin that can't be found. 如果我在我的组件中没有@import hover.css,Angular将不会编译它们,因为它们引用了无法找到的mixin。 I've tried deep linking just the effects I need from hover.css but that's a hornet's nest because those files have downstream dependencies on other parts of the hover library. 我已经尝试深度链接我需要的效果来自hover.css但这是一个大黄蜂的巢,因为这些文件在悬停库的其他部分有下游依赖性。 This obviously isn't specific to hover, but any scenario in which you'd want to import and use a vendor library in an Angular component's CSS file without duplicating the library. 这显然不是特定于悬停,但是您希望在Angular组件的CSS文件中导入和使用供应商库而不复制库的任何情况。

Any ideas? 有任何想法吗?

What do you have on your hover.scss file? 您对hover.scss文件有什么hover.scss Is it only mixins or other CSS as well? 它只是mixins或其他CSS吗? If you only have mixins then you should be fine, if you have some CSS then it will get taken. 如果你只有mixins那么你应该没问题,如果你有一些CSS那么它会被采取。 For example: 例如:

This would not cause repetition: 这不会导致重复:

@mixin underline-from-left {
    text-decoration: underline;
}

If you have something like this, then the span block will be repeated as many times as you would import it: 如果你有这样的东西,那么span块将重复导入它的次数:

@mixin underline-from-left {
    text-decoration: underline;
}

span {
    display: block;
}

Perhaps an idea is to separate mixins from actual CSS, and then import only the mixins file. 也许一个想法是将mixins与实际的CSS分开,然后只导入mixins文件。

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

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