简体   繁体   English

将引导模板与 vanilla Web 组件一起使用

[英]Using bootstrap template with vanilla web components

I have a traditional ASP.NET MVC web site, that uses the Metronic Bootstrap template.我有一个传统的 ASP.NET MVC 网站,它使用 Metronic Bootstrap 模板。

Now, I'm thinking of rewriting this web site using the vanilla web components approach.现在,我正在考虑使用 vanilla web components 方法重写这个网站。

So I did a small test, and wrote a web component without shadow dom, and it seems my component just uses the Metronic style fine, so it inherits it from the parent page where I included it.所以我做了一个小测试,写了一个没有shadow dom的web组件,看起来我的组件只是使用了Metronic风格,所以它从我包含它的父页面继承了它。

However, when I enable shadow DOM, then CSS propagation stops, and now my web component is not using the Metronic style anymore.但是,当我启用 shadow DOM 时,CSS 传播停止,现在我的 Web 组件不再使用 Metronic 样式。

At this time I'm a bit uncertain on how to handle this situation: I can simply not use shadow DOM, but then, I'm not using the full strength and encapsulation of web components.目前我对如何处理这种情况有点不确定:我根本不能使用 shadow DOM,但是,我没有使用 web 组件的全部强度和封装。

So how would you handle the situation where you have a large Bootstrap template that you want to use, in combination with web components?那么,如果您有一个大型 Bootstrap 模板要与 Web 组件结合使用,您将如何处理? Is it worth it?这值得么? Are there guidelines for this kind of situation?是否有针对这种情况的指导方针?

If you need to use a large CSS framework within ShadowDOM based Web Components then you need to include that CSS in each component or, at least, the portion of CSS needed for that component.如果您需要在基于 ShadowDOM 的 Web 组件中使用大型 CSS 框架,那么您需要在每个组件中包含该 CSS,或者至少包含该组件所需的 CSS 部分。

ShadowDOM was designed to prevent outside CSS from affecting the inner ShadowDOM. ShadowDOM 旨在防止外部 CSS 影响内部 ShadowDOM。 So, if you want specific CSS inside the ShadowDOM then you have to put it there.所以,如果你想在 ShadowDOM 中使用特定的 CSS,那么你必须把它放在那里。

The simplest thing to do is to include the <link> tag in your ShadowDOM.最简单的做法是在 ShadowDOM 中包含<link>标签。 The same <link> tag you used outside of the components.您在组件外部使用的相同<link>标记。

 class MyElement extends HTMLElement { constructor() { super(); let shadow = this.attachShadow({mode: 'open'}); shadow.innerHTML = ` <link href="my.css" rel="stylesheet"> <h2 class="my-header">My Component</h2> <button class="my-button">Pay me</button>`; } } customElements.define('my-element', MyElement);
 <link href="my.css" rel="stylesheet"> <div class="my-thing">Stuff</div> <button class="my-button">Click me</button> <hr/> <my-element></my-element>

Since I don't have a file my.css you won't see the changes.因为我没有文件my.css你不会看到变化。

This will allow you to include the CSS in your page and also in your Components.这将允许您在页面和组件中包含 CSS。

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

相关问题 使用 Web 组件的 Vanilla hello world 应用程序 - Vanilla hello world app using web components 自定义 Web 组件 vanilla JavaScript - Custom web components vanilla JavaScript Web Vanilla JS 中的组件:等同于 ngFor 和 v-for? - Web Components in Vanilla JS: Equivalent of ngFor and v-for? 使用 vanilla JS 在 web 组件中切换部分显示/隐藏列表 - Toggle a partial show/hide list in a web components with vanilla JS Vanilla JS Web 组件在 function 的第二次调用中出现意外的未定义属性 - Vanilla JS Web Components unexpected undefined attribute in second call of a function 外部依赖在哪里存在于vanilla JS Web组件中? - Where do external dependencies live in vanilla JS web components? 处理Web组件时的Vanilla JavaScript事件委托 - Vanilla JavaScript event delegation when dealing with Web Components 在 Vanilla JS 中为单个页面创建可重用的 web 组件? - Creating reusable web components for a single page in Vanilla JS? Web组件(香草,无聚合物):如何加载 <template> 内容? - web component (vanilla, no polymer): how to load <template> content? 如何使用Vanilla JavaScript或JQuery实现受控组件(在ReactJS中) - How to implement Controlled Components (in ReactJS) using Vanilla JavaScript or JQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM