# @formio/core 中文文档

规范地址：https://npmcn.com/package/%40formio/core/

版本：2\.9\.1
npm 元数据更新时间：2026-11-13T16:50:20.818Z
中文译文更新时间：2026-11-15T09:03:37.398Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：12.1 MB，624 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- 运行依赖：11 个
- 首次发布：2020-06-12T16:09:03.031Z
- 最新版本发布：2026-11-13T16:50:20.505Z
- 近 30 天下载量：221309（2026-10-16 至 2026-11-14，npm 官方下载统计）
- 已发布版本数：587（近 12 个月 24 个）

## 中文简介

核心Form.io渲染框架.

## 安装

```sh
npm install @formio/core
```

也可以使用：`pnpm add @formio/core`、`yarn add @formio/core`、`bun add @formio/core`

## README

## Form.io 核心数据处理引擎

这个库是Form.io平台背后的核心数据处理引擎. 它是一组异构的API,允许JSON形式和提交定义的复杂管弦(如计算值,有条件隐藏的组件,复杂的逻辑等).

## 正式文件

最新文件、发布资料和指南一律参考本文中提供的官方表格:

**[https://help.form.io (英语)](https://help.form.io/deploy/maintenance-and-migration#new-validation-engine)**

### 使用量

@formio/core作为 npm 包提供. 您可以使用您选择的软件包管理器安装 :

```bash
# npm
npm install --save @formio/core

# yarn
yarn add @formio/core
```

### 发展

有效处理表格和提交数据有两个不同的要求:

1.  (一) JSON表格的形态和数据认识;以及
2.  一组处理功能来得到(并偶尔会变异)形成状态.

第一个要求是通过 `eachComponentData` 财务报告和审定财务报表 `eachComponentDataAsync` 函数,它跨越每个组成元件JSON,并提供一个调用参数,通过这个参数与元件相互作用,它是相应的提交数据参数.

第二个要求是通过"处理器"(例如. `calculate`, (中文(简体) ), `validate`,或 `hideChildren`),这些职能,经评价后 `context`,运行、从状态中得出,并偶尔根据内部形式逻辑对形式状态和提交值进行突变,从而形成 `scope` 对象,包含每个按组件路径键的处理器的结果.

在表格和呈件上运行一套处理器 `process` 函数的家族形式包括:JSON定义、提交JSON定义和一系列处理器函数作为参数(概括为 `context` 对象,它传递给每个调用回调处理器函数.

```js
import { processSync } from '@formio/core';

const form = {
  display: 'form',
  components: [
    {
      type: 'textfield',
      key: 'firstName',
      label: 'First Name',
      input: true,
    },
    {
      type: 'textfield',
      key: 'lastName',
      label: 'Last Name',
      input: true,
    },
    {
      type: 'button',
      key: 'submit',
      action: 'submit',
      label: 'Submit',
    },
  ],
};

const submission = {
  data: {
    firstName: 'John',
    lastName: 'Doe',
  },
};

const addExclamationSync = (context) => {
  const { component, data, scope, path, value } = context;

  if (!scope.addExclamation) scope.addExclamation = {};
  let newValue = `${value}!`;

  // The scope is a rolling "results" object that tracks which components have been operated on by which processor functions
  scope.addExclamation[path] = true;
  _.set(data, path, newValue);
  return;
};

// The context object is mutated depending on which component is being processed; after `processSync` it will contain the processed components and data
const context = {
  components: form.components,
  data: submission.data,
  processors: [{ processSync: addExclamationSync }],
  scope: {},
};

// The `process` family of functions returns the scope object
const resultScope = processSync(context);

console.assert(resultScope['addExclamation']?.firstName === true);
console.assert(resultScope['addExclamation']?.lastName === true);
console.assert(submission.data.firstName === 'John!');
console.assert(submission.data.lastName === 'Doe!');
```

### 实验

这个库包含实验代码(发现于 `src/experimental` 目录或通过导入,例如:. `import { Components } from @formio/core/experimental`),旨在更新并取代Form.io平台后的核心渲染引擎. 它是一个微小(12k gzipped)的渲染框架,可以渲染出复杂的组件并管理由每个组件所控制的数据模型.

#### 使用量

要使用此实验框架, 您首先需要将父库安装到您的应用程序中 .

```bash
# npm
npm install --save @formio/core
# yarn
yarn add @formio/core
```

接下来,您可以创建如下新组件 .

```js
import { Components } from '@formio/core/experimental';
Components.addComponent({
  type: 'h3',
  template: (ctx) => `<h3>${ctx.component.header}</h3>`,
});
```

现在这个组件将使用以下方法来渲染.

```js
const header = Components.createComponent({
  type: 'h3',
  header: 'This is a test',
});
console.log(header.render()); // Outputs <h3>This is a test</h3>
```

您也可以通过将以下内容纳入您的网页脚本来使用此库 .

```
<script src="https://cdn.jsdelivr.net/npm/@formio/base@latest/dist/formio.core.min.js"></script>
```

在此之后,您可以在您的网站上创建数据表.

```js
FormioCore.render(
  document.getElementById('data-table'),
  {
    type: 'datatable',
    key: 'customers',
    components: [
      {
        type: 'datavalue',
        key: 'firstName',
        label: 'First Name',
      },
      {
        type: 'datavalue',
        key: 'lastName',
        label: 'First Name',
      },
    ],
  },
  {},
  {
    customers: [
      { firstName: 'Joe', lastName: 'Smith' },
      { firstName: 'Sally', lastName: 'Thompson' },
      { firstName: 'Mary', lastName: 'Bono' },
    ],
  },
);
```

## 常见问题

### @formio/core 是什么？

核心Form\.io渲染框架\.

### 如何安装 @formio/core？

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

### @formio/core 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/formio\_\_core 是否存在。

### @formio/core 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @formio/core 可以商用吗？

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

### @formio/core 的下载量有多少？

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

### @formio/core 还在维护吗？

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

