什么是 web components ?
Web Components 是一个浏览器原生支持的组件化方案,允许你创建新的自定义、可封装、可重用的HTML 标记。不用加载任何外部模块,直接就可以在浏览器中跑。
它的出现原因?因为早期组件生态很乱,有各种各样的框架和库,都有各自的规范,导致一个团队切换框架很困难 。为了解决这种分化的形式,让 Web 组件模型统一化,所以才有了Web Components规范的出现。目标是提供一种权威的、浏览器能理解的方式来创建组件。
学习 web components 有什么用 ?
用一句话总结就是回顾历史展望未来。
2011年提出Web Components概念,React诞生
2013年 Chrome 和 Opera 又联合提出了推出的 V0 版本的 Web Components 规范,React开源
2014年Vue诞生
2016年Web Components 推进到了 V1 版本
由于浏览器兼容性、和主流框架开发效率等等问题导致现在几乎使用不到它,但我们可以学习它的思想,也许未来就会变的有用?
Web Components 由三种技术组成
custom Elements
可以创建一个自定义标签。根据规范,自定义元素的名称必须包含连词线”-“,用与区别原生的 HTML 元素。
class UserCard extends HTMLElement { constructor() { super(); var el = document.createElement('p'); el.classList.add('name'); el.innerText = 'User Name'; this.append(el); } } window.customElements.define('user-card', UserCard);
Shadow DOM
Shadow DOM是对DOM的一个封装。可以将标记结构、样式和行为隐藏起来,并与页面上的其他代码相隔离,保证不同的部分不会混在一起,可使代码更加干净、整洁。
p { color: blueviolet; }不会影响shadow样式
const header = document.createElement('header'); const shadowRoot = header.attachShadow({mode: 'closed'});// header.shadowRoot === null // const shadowRoot = header.attachShadow({mode: 'open'});// header.shadowRoot === shadowRoot #document-fragment shadowRoot.innerHTML = 'Hello Shadow DOM
'; document.querySelector('body').append(header);// shadowRoot.host === header
templates and slots
可以编写不在呈现页面中显示的标记模板。然后它们可以作为自定义元素结构的基础被多次重用。
被使用前不会被渲染。
被使用前对页面其他部分没有影响,脚本不会运行,图像不会加载,音频不会播放。
会影响外部样式
p{color: red;}My paragraph
customElements.define('my-paragraph', class extends HTMLElement { constructor() { super(); let template = document.getElementById('my-paragraph'); let templateContent = template.content.cloneNode(true); this.appendChild(templateContent); } })
slots的使用:
p{color: blueviolet;}会影响外部样式
p{color: red;}My paragraph
My default textslot text
customElements.define('my-paragraph', class extends HTMLElement { constructor() { super(); let template = document.getElementById('my-paragraph'); let templateContent = template.content.cloneNode(true); this.attachShadow({mode: 'open'}).appendChild(templateContent); } })
生命周期钩子
使用生命周期回调函数
在custom element的构造函数中,可以指定多个不同的回调函数,它们将会在元素的不同生命时期被调用:
connectedCallback:当 custom element首次被插入文档DOM时,被调用。 disconnectedCallback:当 custom element从文档DOM中删除时,被调用。 adoptedCallback:当 custom element被移动到新的文档时,被调用。 attributeChangedCallback: 当 custom element增加、删除、修改自身属性时,被调用。
具体实践
代码结构:
代码:
// index.html web components
第一种写法:
// btn.js class Btn extends HTMLElement { constructor() { super(); const shaDow = this.attachShadow({ mode: "closed" }); this.p = this.h("p"); this.p.innerText = "王乐乐"; this.p.setAttribute( "style", "width:200px;height:50px;border:1px solid red;" ); shaDow.append(this.p); } h(el) { return document.createElement(el); } } window.customElements.define("wll-btn", Btn);
效果:
第二种写法:
// btn.js class Btn extends HTMLElement { constructor() { super(); const shaDow = this.attachShadow({ mode: "closed" }); this.template = this.h("template"); this.template.innerHTML = ` div { width:100px; height:100px; border:1px solid green; } 哈喽 `; shaDow.append(this.template.content.cloneNode(true)); } h(el) { return document.createElement(el); } } window.customElements.define("wll-btn", Btn);
效果:
在vue项目里使用 web components
- 配置 vite.config.ts
plugins: [ vue({ template: { compilerOptions: { // 当组件命名是以 wll- 开头时便跳过 vue 机制检测 isCustomElement: (tag) => tag.includes('wll-') } } }) ],
- 书写自定义组件
// CustomVue.ce.vue // 注意,必须以 .ce.vue 结尾leleledefineProps obj:any }()
- 根组件引入
// App.vue import { defineCustomElement } from 'vue'; import CustomVueCe from '@/components/CustomVue.ce.vue'; const Btn = defineCustomElement(CustomVueCe); window.customElements.define('wll-btn', Btn);
效果: