1、为啥会有这一篇 ?前两天有个同学问我,然后组内同学也即将分享相关实践内容,此处 在他之前发出(早一天发布的发布会),good!
2、是何物?组件化是前端非常重要的一块内容,现在流行的 React 和 Vue 都是组件框架。
谷歌 Chrome 一直在推动浏览器的原生组件,即 Web Components API。相比第三方框架,原生组件简单直接,不用加载任何外部模块,代码量小。当前发展较为成熟,在很多项目中被使用
广受好评!
3、基本概念早知道Web Components主要由三项技术组成,分别为
Custom elements(自定义元素)
Shadow DOM(影子DOM)
HTML templates(HTML模板)
自定义元素实例
customElements.define(
'test-custom', // name
class TestCustom extends HTMLParagraphElement { // 这个类定义功能constructor() {super();// 功能代码...}
}, { extends: 'p' }); // 继承自哪个元素 此处 p
Shadow DOM 可将隐藏的 DOM挂到一个元素上:且 shadow root 节点为起始根节点,在这个根节点的下方,可以是任意元素,和普通的 DOM 元素一样
附加到 custom element 怎么做?class Button extends HTMLElement {constructor() {super();let shadow = this.attachShadow({mode: 'open'}); // 挂到 构造函数上}
}
class Button extends HTMLElement {constructor() {super();let shadow = this.attachShadow({mode: 'open'});let p = document.createElement('p');shadow.appendChild(p);}
}
let mySelfStyle = document.createElement('style');
mySelfStyle.textContent = `.btn-container {position: relative;}.btn {// ...}
`
shadow.appendChild(mySelfStyle);
4、磨拳擦掌,怎么使用 ?1.创建一个类或函数来指定 web 组件的功能,如果使用类,请使用 ECMAScript 2015 的类语法 (参阅类获取更多信息)。
2.使用 CustomElementRegistry.define() 方法注册您的新自定义元素,并向其传递要定义的元素名称、指定元素功能的类、以及可选的其所继承自的元素。
3.如果需要的话,使用Element.attachShadow() 方法将一个 shadow DOM 附加到自定义元素上。使用通常的 DOM 方法向 shadow DOM 中添加子元素、事件监听器等等。
4.如果需要的话,使用 和 定义一个 HTML 模板。再次使用常规 DOM 方法克隆模板并将其附加到您的 shadow DOM 中。
5.在页面任何位置使用自定义元素,就像使用HTML DIV一样(为例)
5、实现一个 Button1、创建一个类来指定 web 组件的功能
customElements.define('mark-button', Button);
2、使用Element.attachShadow() 方法将一个 shadow DOM 附加到自定义元素上
const shadowRoot = this.attachShadow({ mode: 'open' });const btn = document.createlement('button');btn.appendChild(templateContent.cloneNode(true));btn.setAttribute('class', 'mark-button');
button contentxxx
4、使用
button test1
button test2
button test3
5、添加点击事件
document.querySelector('#btn_show').addEventListener('click', () => {modal.setAttribute('visible', 'true');
}, false)
6、效果
// index.html
Web Component Web Component 马克付
1. Button
button test1 button test2 button test3 button content xxx
// Button.js
class Button extends HTMLElement {constructor() {super();// 获取模板内容let template = document.getElementById('btn_tpl');let templateContent = template.content;const shadowRoot = this.attachShadow({ mode: 'open' });const btn = document.createElement('button');btn.appendChild(templateContent.cloneNode(true));btn.setAttribute('class', 'mark-button');// 定义类型primary | warning | defaultconst type = {'primary': '#06c','warning': 'red','default': '#f0f0f0'}const btnType = this.getAttribute('type') || 'default';const btnColor = btnType === 'default' ? 'pink' : '#fff';// 创建样式const style = document.createElement('style');// 为shadow Dom添加样式style.textContent = `.mark-button {position: relative;display: inline-block;padding:8px 20px;border-radius: 50px;margin-right: 3px;background-color: ${type[btnType]};color: ${btnColor};box-shadow: inset 0 5px 10px rgba(0,0,0, .3);cursor: pointer;}`shadowRoot.appendChild(style);shadowRoot.appendChild(btn);}
}
customElements.define('mark-button', Button);
最近还整理一份JavaScript与ES的笔记,一共25个重要的知识点,对每个知识点都进行了讲解和分析。能帮你快速掌握JavaScript与ES的相关知识,提升工作效率。




有需要的小伙伴,可以点击下方卡片领取,无偿分享