Angular 8.3.9-为什么在创建Custom元素时BootStrap3全局样式消失了?

问题描述 投票:0回答:1

我在一个简单的Angular 8应用程序中使用Bootstrap 3,该应用程序包含2个组件。纯粹是为了测试样式如何纳入自定义元素。

1个组件在各自组件的.css文件中具有样式,其他组件使用在angular.json文件中声明的引导样式。 (样式)。

一旦构建完成,我将每个自定义标签放置到一个简单的index.html中,结果是组件CSS中的样式保留并按预期显示,而带有引导程序的组件样式,看起来都是纯HTML ...没有样式?

我正在关注this tutorial

此样式样式一劳永逸。

[为了使它看起来正确,我将bootstrap.min.css添加到index.html中的href中,并且可以正常工作,但是Angular一定可以编译JS,以包括从全局定义的angular.json样式中使用的样式吗?] >

//The one with styling in its contact-form.component.css file
    const myCustomElement = createCustomElement(ContactFormComponent, { injector });
    customElements.define('my-contact-form', myCustomElement);

//The with Bootstrap styling
    const myAnotherElement = createCustomElement(AnotherCompComponent, {injector});
    customElements.define('another-form', myAnotherElement);
Running This---
ng build --prod --output-hashing none && node concatenate.js
concatenate purely concates the main,scripts,polyfills and runtime js files into one
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Testing our custom element</title>
  <base href="/">
</head>
<body>
  <my-contact-form></my-contact-form>
  <another-form></another-form>
  <script type="text/javascript" src="contact-form.js"></script>
</body>
</html>

我希望看到两种形式都显示有样式,这是运行ng serve的结果。

[添加href =“ bootstrap.min.css”进入index.html文件,然后根据需要显示第二个标签。

因此,简而言之...在创建自定义标签时,有一种方法可以使angular包含angular.json声明的样式。

我希望所有这些都有意义。谢谢大家。

我正在一个简单的Angular 8应用程序中使用Bootstrap 3,该应用程序包含2个组件。纯粹是为了测试样式如何融入自定义元素。 1个组件在.css文件中具有相应的样式...

angular element web-component styling custom-component
1个回答
0
投票

目前,我的Web组件中的样式存在相同的问题。我使用Angular8。在进行角度构建之后,它将生成所有可以连接的.js文件。当前,它还生成带有第三方库样式(bootstrap / material等)的单个styles.css文件。目前,对我来说唯一可行的方法是在使用WebComponent的客户端html中包含此样式表。如果有某种方法可以将这些组件作为一个捆绑包(包含所有组件)一起运送,那就太好了。

© www.soinside.com 2019 - 2024. All rights reserved.