首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Web Components 实战:不依赖框架的原生组件化开发

Web Components 实战:不依赖框架的原生组件化开发

作者头像
佛系豪豪吖
发布2026-06-22 21:03:54
发布2026-06-22 21:03:54
970
举报

Web Components 是浏览器原生支持的组件化方案。它不依赖任何框架,写出来的组件可以在 Vue、React 甚至纯 HTML 中无缝使用。

## 三个核心技术

- **Custom Elements** — 自定义 HTML 标签 - **Shadow DOM** — 样式隔离,组件内部样式不污染外部 - **HTML Templates** — 可复用的 DOM 片段

## 最小示例:一个倒计时组件

```javascript class CountdownTimer extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); }

connectedCallback() { const seconds = parseInt(this.getAttribute('seconds')) || 60; this.render(seconds); }

render(seconds) { this.shadowRoot.innerHTML = `

${this.formatTime(seconds)}

`; this.startCountdown(seconds); }

formatTime(s) { const m = Math.floor(s / 60); return `${String(m).padStart(2, '0')}:${String(s % 60).padStart(2, '0')}`; }

startCountdown(remaining) { this._timer = setInterval(() => { remaining--; const display = this.shadowRoot.querySelector('.timer'); if (display) display.textContent = this.formatTime(remaining); if (remaining <= 0) { clearInterval(this._timer); this.dispatchEvent(new CustomEvent('timeup')); } }, 1000); } disconnectedCallback() { clearInterval(this._timer); } } customElements.define('countdown-timer', CountdownTimer); ``` 只需一行 HTML 就能使用: ```html ```

## Shadow DOM 样式隔离实战

```javascript class UserCard extends HTMLElement { constructor() { super(); const template = document.getElementById('user-card-tpl'); this.attachShadow({ mode: 'open' }) .appendChild(template.content.cloneNode(true)); }

static get observedAttributes() { return ['name', 'avatar', 'role']; }

attributeChangedCallback(name, oldVal, newVal) { const el = this.shadowRoot?.querySelector(`[slot="${name}"]`); if (el) el.textContent = newVal; } } ```

## 何时用 Web Components?

| 场景 | 推荐 | |------|------| | 多个项目共用组件(不同框架) |

最合适 | | 公司内部组件库 |

| | 微前端方案的组件层 |

| | 单一 Vue/React 项目 | 用框架自身的组件即可 | | 需要 SSR | 需要搭配 Lit + SSR |

Web Components 不是要取代 React/Vue,而是在跨框架复用场景下给了你一个原生、无依赖的选择。

---

> 本文由 AI 辅助创作

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-06-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档