# @atomico/channel 中文文档

规范地址：https://npmcn.com/package/%40atomico/channel/

版本：0\.0\.1
npm 元数据更新时间：2022-05-08T20:57:44.558Z
中文译文更新时间：2026-11-08T08:40:47.678Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：5.9 kB，5 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2021-06-14T02:22:23.101Z
- 最新版本发布：2022-05-08T20:57:44.468Z
- 近 30 天下载量：1850（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：2（近 12 个月 0 个）

## 中文简介

在WebComputers或DOM之间轻松共享状态.

## 安装

```sh
npm install @atomico/channel
```

也可以使用：`pnpm add @atomico/channel`、`yarn add @atomico/channel`、`bun add @atomico/channel`

## README

# @ 原子/ 频道

`@atomico/channel` 允许WebComputers或DOM之间轻松共享状态.

![Example](https://i.ibb.co/DfWvg85/Grupo-38.png)

这个API的灵感来自React Context的目标,但消除了复杂性,是一种不可知的执行.

## 什么是频道?

一个频道是发送者和通过 DOM 的本地事件系统获得状态的接收者,一个频道有以下逻辑:

1.  读取父的上下文
2.  修改儿童的上下文

## 示例

这是网络组件订阅父节点的例子 :

```js
import { Channel } from "@atomico/channel";

const CHANNEL = "MyChannel";
// Parent channel
const parentChannel = new Channel(document.body, CHANNEL);

class MyComponent extends HTMLElement {
    constructor() {
        super();
        // Child channel
        this.channel = new Channel(this, CHANNEL);
    }
    connectedcallback() {
        this.channel.connected(
            (data) => (this.textContent = JSON.stringify(data))
        );
    }
    disconnectedCallback() {
        this.channel.disconnect();
    }
}

// Connect the channel to the native DOM event system
parentChannel.connect();

parentChannel.cast("I'm your father");
```

## API 密码

### 频道( )

```js
const channel = new Channel(
    // Element
    host,
    // string
    "idString",
    // associates the composed option to the event
    // this allows bypassing the shadowDOM when connecting the channels
    true
);
```

### 频道. connect ()

```js
channel.connect(optionalCallback);
```

地点:

1.  `optionalCallback`:可选择的回调,允许读取父的传输.

### 频道. disconect ()

删除订阅

### 频道.cast ()

向儿童频道播放新节目

```js
channel.cast(10);
channel.cast({ data });
```

执行这种方法可以防止从父方传出的任何传输向该通道的孩子传播.

## 常见问题

### @atomico/channel 是什么？

在WebComputers或DOM之间轻松共享状态\.

### 如何安装 @atomico/channel？

在项目目录运行 npm install @atomico/channel，或使用 pnpm add / yarn add / bun add。

### @atomico/channel 支持 TypeScript 吗？

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

### @atomico/channel 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @atomico/channel 可以商用吗？

@atomico/channel 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @atomico/channel 的下载量有多少？

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

### @atomico/channel 还在维护吗？

最新版本 0\.0\.1 发布于 2022/05/09（4 年前）；至今共发布 2 个版本，其中过去 12 个月发布了 0 个；首次发布于 2021/06/14。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

