深入理解 web components

本文介绍了WebComponents,一种浏览器原生支持的组件化解决方案,旨在解决早期组件生态混乱的问题。文章详细阐述了customElements、ShadowDOM、模板和slots等组成部分,以及它们的作用和生命周期钩子。尽管目前应用有限,但学习其思想对未来可能有价值。
摘要由CSDN通过智能技术生成

什么是 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 元素。

<body>
    <user-card></user-card>
    <script>
        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);
    </script>
</body>

Shadow DOM
Shadow DOM是对DOM的一个封装。可以将标记结构、样式和行为隐藏起来,并与页面上的其他代码相隔离,保证不同的部分不会混在一起,可使代码更加干净、整洁。

<style>
    p {
        color: blueviolet;
    }
</style>

<p>不会影响shadow样式</p>
<script>
    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 = '<p>Hello Shadow DOM</p>';
    document.querySelector('body').append(header);// shadowRoot.host === header
</script>

templates and slots
可以编写不在呈现页面中显示的标记模板。然后它们可以作为自定义元素结构的基础被多次重用。

被使用前不会被渲染。
被使用前对页面其他部分没有影响,脚本不会运行,图像不会加载,音频不会播放。

<body>
    <p>会影响外部样式</p>
    <template id="my-paragraph">
        <style>
            p{color: red;}
        </style>
        <p>My paragraph</p>
    </template>
    <my-paragraph></my-paragraph>
    <script>
        customElements.define('my-paragraph',
            class extends HTMLElement {
                constructor() {
                    super();
                    let template = document.getElementById('my-paragraph');
                    let templateContent = template.content.cloneNode(true);

                    this.appendChild(templateContent);
                }
        })
    </script>
</body>

slots的使用:

<body>
    <style>
        p{color: blueviolet;}
    </style>
    
    
    <p>会影响外部样式</p>
    <template id="my-paragraph">
        <style>
            p{color: red;}
        </style>
        <p>My paragraph</p>
        <slot name="my-text">My default text</slot>
    </template>
    
    
    <my-paragraph>
        <p slot="my-text">slot text</p>
    </my-paragraph>
    
    
    <script>
        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);
                }
        })
    </script>
</body>

生命周期钩子

使用生命周期回调函数
在custom element的构造函数中,可以指定多个不同的回调函数,它们将会在元素的不同生命时期被调用:

connectedCallback:当 custom element首次被插入文档DOM时,被调用。
disconnectedCallback:当 custom element从文档DOM中删除时,被调用。
adoptedCallback:当 custom element被移动到新的文档时,被调用。
attributeChangedCallback: 当 custom element增加、删除、修改自身属性时,被调用。

具体实践

代码结构:
在这里插入图片描述
代码:

// index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>web components</title>
    <script src="./btn.js"></script>
  </head>
  <body>
    <wll-btn></wll-btn>
  </body>
</html>

第一种写法:

// 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 = `
        <style>
            div {
                width:100px;
                height:100px;
                border:1px solid green;
            }
        </style>
        <div>
            哈喽
        </div>
      `;

    shaDow.append(this.template.content.cloneNode(true));
  }

  h(el) {
    return document.createElement(el);
  }
}

window.customElements.define("wll-btn", Btn);

效果:
在这里插入图片描述

在vue项目里使用 web components

  1. 配置 vite.config.ts
plugins: [
    vue({
      template: {
        compilerOptions: {
          // 当组件命名是以 wll- 开头时便跳过 vue 机制检测 
          isCustomElement: (tag) => tag.includes('wll-')
        }
      }
    })
  ],
  1. 书写自定义组件
// CustomVue.ce.vue // 注意,必须以 .ce.vue 结尾
<template>
  <div>lelele</div>
</template>

<script setup lang="ts">
defineProps<{
	obj:any
}>()
</script>

<style scoped></style>

  1. 根组件引入
// App.vue
<template>
  <wll-btn :obj="JSON.stringify(obj)"></wll-btn>
</template>

<script setup lang="ts">
import { defineCustomElement } from 'vue';
import CustomVueCe from '@/components/CustomVue.ce.vue';

const Btn = defineCustomElement(CustomVueCe);

window.customElements.define('wll-btn', Btn);
</script>

<style scoped lang="less"></style>

效果:
在这里插入图片描述

  • 48
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WongLeer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值