简体   繁体   English

Angular Web 组件,如何将css文件嵌入到最终的js文件中?

[英]Angular Web Component, how to embed css file into the final js file?

I have a simple web component that I created with Angular.我有一个使用 Angular 创建的简单 web 组件。 I am able to build it into one JavaScript and one css file and run it on another web page.我可以将它构建到一个 JavaScript 和一个 css 文件中,并在另一个 web 页面上运行它。

How do I embed the css file into the js file?如何将css文件嵌入到js文件中?

I'm confident that it can be done, but I cannot find any answers of how to do it.我相信它可以做到,但我找不到任何关于如何做到这一点的答案。

The Angular project is setup to use css files. Angular 项目设置为使用 css 文件。 It is also using Angular Materials.它还使用 Angular 材料。 The idea of defining all styles in the component files came to me.我想到了在组件文件中定义所有 styles 的想法。 Not sure if that will result in the outcome I'm looking for though.不确定这是否会导致我正在寻找的结果。

EDIT: To get to this point, I mainly followed the steps laid out in this tutorial.编辑:为了达到这一点,我主要遵循教程中列出的步骤。

I have now found a few tutorials that talk about changing the webpack.config file in the Angular project, but that file does not exist and I doubt it will auto-magically take it into account if I add it.我现在找到了一些关于在 Angular 项目中更改 webpack.config 文件的教程,但是该文件不存在,我怀疑如果我添加它会自动将其考虑在内。

Since you are talking about "web components", for definition, I suppose you are using the shadow Dom, so setting the encapsulation of your angular component to shadowDom(the default is emulated).既然您在谈论“Web 组件”,为了定义,我想您正在使用影子 Dom,因此将 angular 组件的封装设置为 shadowDom(默认为模拟)。 When you use Shadow Dom all css and html elements(Dom) are "incapsulated" inside the component(Shadow root) for avoiding conflicts with external elements.当您使用 Shadow Dom 时,所有 css 和 html 元素(Dom)都“封装”在组件(Shadow root)内部,以避免与外部元素发生冲突。 So your CSS code contained inside your component cannot affect from inside other elements, and cannot be affected from outside: this is a normal behaviour of web components.因此,您的组件中包含的 CSS 代码不会受到其他元素内部的影响,也不会受到外部影响:这是 web 组件的正常行为。 I think that you don't need the single CSS file, since you can compile your web component into a single js that contains everything inside.我认为您不需要单个 CSS 文件,因为您可以将 web 组件编译为包含内部所有内容的单个 js。

If you want to manipulate the style of your component, you can use some @input property: for example: @input() background-color , set the logic to change the background colour dynamically and when call the component from external, pass the input property:如果要操作组件的样式,可以使用一些 @input 属性:例如: @input() background-color ,设置动态更改背景颜色的逻辑,当从外部调用组件时,传递输入财产:

<my-web-component [background-color]="red"/>

You can even define some CSS classes inside the component, and using the same mechanism, you can decide which of this classes to use, when your web component is being used outside of your angular application.您甚至可以在组件内定义一些 CSS 类,并使用相同的机制,当您的 web 组件在您的 ZD18B8624A0F5F721DDZDA7B82365FC56 应用程序之外使用时,您可以决定使用哪个类。

EDIT: How to setup shadow DOM in angular :编辑:如何在 angular 中设置阴影 DOM

 @Component({ selector: 'my-component', templateUrl: './my-component.component.html', styleUrls: ['./my-component.component.css'], //just add this row on you component metadata decorator encapsulation: ViewEncapsulation.None //default is ViewEncapsulation.Emulated })

Regarding webpack take a look at this Angular CLI extension: https://github.com/manfredsteyer/ngx-build-plus关于 webpack 看看这个 Angular CLI 扩展: https://github.com/manfredsteyer/ngx-build-plus

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

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