# @beforesemicolon/web-component 中文文档

规范地址：https://npmcn.com/package/%40beforesemicolon/web-component/

版本：1\.21\.1
npm 元数据更新时间：2026-11-01T11:46:25.660Z
中文译文更新时间：2026-11-07T20:44:21.120Z
软件包许可：BSD-3-Clause

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40beforesemicolon/web-component
原文对照：https://npmcn.com/package/%40beforesemicolon/web-component/?lang=original
项目主页：https://github.com/beforesemicolon/web-component#readme
源码仓库：https://github.com/beforesemicolon/web-component

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：80.8 kB，40 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=22\.13\.0
- 运行依赖：1 个
- 首次发布：2021-10-17T23:55:18.052Z
- 最新版本发布：2026-11-01T11:46:25.487Z
- 近 30 天下载量：3110（2026-10-08 至 2026-11-06，npm 官方下载统计）
- 已发布版本数：110（近 12 个月 13 个）

## 中文简介

具有创建本地Web组件能力的增强标记

## 安装

```sh
npm install @beforesemicolon/web-component
```

也可以使用：`pnpm add @beforesemicolon/web-component`、`yarn add @beforesemicolon/web-component`、`bun add @beforesemicolon/web-component`

## README

# 网络组件

[![Static Badge](https://img.shields.io/badge/documentation-blue)](https://web-component.beforesemicolon.com/) [![Test](https://github.com/beforesemicolon/web-component/actions/workflows/test.yml/badge.svg)](https://github.com/beforesemicolon/web-component/actions/workflows/test.yml) [![npm](https://img.shields.io/npm/v/%40beforesemicolon%2Fweb-component)](https://www.npmjs.com/package/@beforesemicolon/web-component) ![npm](https://img.shields.io/npm/l/%40beforesemicolon%2Fweb-component)

增强网络组件 API [标记](https://markup.beforesemicolon.com/)。 。 。 .

## 动机

-   原生的Web组件API太强了. 这意味着你需要为最简单的组件写这么多代码.
-   即使你能够处理好所有的API,你仍然需要处理DOM操纵,并处理你自己的回放.
-   [标记](https://markup.beforesemicolon.com/) 提供最简单、最强大的诱导系统 在客户端上没有设置.

由于所有这些原因,引入一个简单的API来为您处理所有的事情是完全合理的.

```ts
// import everything from Markup as if you are using it directly
import { WebComponent, html } from '@beforesemicolon/web-component'
import stylesheet from './counter-app.css' with { type: 'css' }

interface Props {
    label: string
}

interface State {
    count: number
}

class CounterApp extends WebComponent<Props, State> {
    static observedAttributes = ['label']
    label = '+' // defined props default value
    initialState = {
        // declare initial state
        count: 0,
    }
    stylesheet = stylesheet

    countUp = (e: Event) => {
        e.stopPropagation()
        e.preventDefault()

        this.setState(({ count }) => ({ count: count + 1 }))
        this.dispatch('click')
    }

    render() {
        return html`
            <p>${this.state.count}</p>
            <button type="button" onclick="${this.countUp}">
                ${this.props.label}
            </button>
        `
    }
}

customElements.define('counter-app', CounterApp)
```

在您的 HTML 中, 您可以简单地正常使用标记 .

```html
<counter-app label="count up"></counter-app>
```

## 安装

```
npm install @beforesemicolon/web-component
```

在浏览器中

```html
<!-- use the latest version -->
<script src="https://unpkg.com/@beforesemicolon/web-component/dist/client.js"></script>

<!-- use a specific version -->
<script src="https://unpkg.com/@beforesemicolon/web-component@1.21.0-next/dist/client.js"></script>

<!-- link you app script after -->
<script>
    const { WebComponent } = BFS
    const { html, state, effect } = BFS.MARKUP
</script>
```

## AI和生态系统背景

-   [`llms.txt`](https://web-component.beforesemicolon.com/llms.txt) 是简洁的, 软件包拥有的API和AI工具的生态系统合同.
-   [`llms-full.txt`](https://web-component.beforesemicolon.com/llms-full.txt) 包含完整的解析文档和实例.

Web 组件再出口 Markup. 路由器和 Intl 在 Web 组件上构建并可以 在不改变其组成部分模式的情况下合并组成.

## 社区

-   [贡献准则](https://npmcn.com/package/%40beforesemicolon/web-component/CONTRIBUTING.md)
-   [行为守则](https://npmcn.com/package/%40beforesemicolon/web-component/CODE_OF_CONDUCT.md)
-   [安全政策](https://npmcn.com/package/%40beforesemicolon/web-component/SECURITY.md)
-   [BSD 3- 标签许可证](https://npmcn.com/package/%40beforesemicolon/web-component/LICENSE)

## 常见问题

### @beforesemicolon/web-component 是什么？

具有创建本地Web组件能力的增强标记

### 如何安装 @beforesemicolon/web-component？

在项目目录运行 npm install @beforesemicolon/web-component，或使用 pnpm add / yarn add / bun add。

### @beforesemicolon/web-component 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @beforesemicolon/web-component 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @beforesemicolon/web-component 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=22\.13\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @beforesemicolon/web-component 可以商用吗？

@beforesemicolon/web-component 使用 BSD-3-Clause 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括，不构成法律意见，请以许可原文为准。

### @beforesemicolon/web-component 的下载量有多少？

根据 npm 官方下载统计，@beforesemicolon/web-component 在 2026/10/08 – 2026/11/06 这 30 天内被下载 3,110 次，日均约 104 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @beforesemicolon/web-component 还在维护吗？

最新版本 1\.21\.1 发布于 2026/11/01（6 天前）；至今共发布 110 个版本，其中过去 12 个月发布了 13 个；首次发布于 2021/10/18。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

