# 组件与动态区域

> Source: https://strapi.nodejs.cn/cms/api/entity-service/components-dynamic-zones

🌐 Creating components and dynamic zones with the Entity Service API

使用实体服务 API 在创建或更新条目时创建和更新组件及动态区域。组件是单个对象，而动态区域是具有 `__component` 类型标识符的组件列表。

🌐 Use the Entity Service API to create and update components and dynamic zones while creating or updating entries. Components are single objects while dynamic zones are lists of components with a `__component` type identifier.

:::caution

在 Strapi v5 中，实体服务 API 已被弃用。请考虑改用 [文档服务 API](/cms/api/document-service)。

🌐 The Entity Service API is deprecated in Strapi v5. Please consider using the [Document Service API](/cms/api/document-service) instead.

:::

[实体服务](/cms/api/entity-service) 是处理 [组件](/cms/backend-customization/models#components-json) 和 [动态区域](/cms/backend-customization/models#dynamic-zones) 逻辑的层。通过实体服务 API，可以在创建或更新条目时 [创建](#creation) 和 [更新](#update) 组件和动态区域。

🌐 The [Entity Service](/cms/api/entity-service) is the layer that handles [components](/cms/backend-customization/models#components-json) and [dynamic zones](/cms/backend-customization/models#dynamic-zones) logic. With the Entity Service API, components and dynamic zones can be [created](#creation) and [updated](#update) while creating or updating entries.

## 创造 {#creation}

🌐 Creation

在使用实体服务 API 创建条目时，可以创建一个 [组件](/cms/backend-customization/models#components-json)：

🌐 A [component](/cms/backend-customization/models#components-json) can be created while creating an entry with the Entity Service API:

```js
strapi.entityService.create('api::article.article', {
  data: {
    myComponent: {
      foo: 'bar',
    },
  },
});
```

在使用实体服务 API 创建条目时，可以创建一个[动态区域](/cms/backend-customization/models#dynamic-zones)（即组件列表）:

🌐 A [dynamic zone](/cms/backend-customization/models#dynamic-zones) (i.e. a list of components) can be created while creating an entry with the Entity Service API:

```js
strapi.entityService.create('api::article.article', {
  data: {
    myDynamicZone: [
      {
        __component: 'compo.type',
        foo: 'bar',
      },
      {
        __component: 'compo.type2',
        foo: 'bar',
      },
    ],
  },
});
```

## 更新 {#update}

🌐 Update

在使用实体服务 API 更新条目时，可以更新一个 [组件](/cms/backend-customization/models#components-json)。如果指定了组件 `id`，则组件会被更新，否则旧的组件会被删除并创建一个新的组件：

🌐 A [component](/cms/backend-customization/models#components-json) can be updated while updating an entry with the Entity Service API. If a component `id` is specified, the component is updated, otherwise the old one is deleted and a new one is created:

```js
strapi.entityService.update('api::article.article', 1, {
  data: {
    myComponent: {
      id: 1, // will update component with id: 1 (if not specified, would have deleted it and created a new one)
      foo: 'bar',
    },
  },
});
```

使用实体服务 API 更新条目时，动态区域（即组件列表）可以被更新。如果指定了组件 `id`，则该组件会被更新，否则旧的组件会被删除并创建一个新的组件：

🌐 A [dynamic zone](/cms/backend-customization/models#dynamic-zones) (i.e. a list of components) can be updated while updating an entry with the Entity Service API. If a component `id` is specified, the component is updated, otherwise the old one is deleted and a new one is created:

```js
strapi.entityService.update('api::article.article', 1, {
  data: {
    myDynamicZone: [
      {
        // will update
        id: 2,
        __component: 'compo.type',
        foo: 'bar',
      },
      {
        // will add a new & delete old ones
        __component: 'compo.type2',
        foo: 'bar2',
      },
    ],
  },
});
```
