Skip to main content
Unlisted page
This page is unlisted. Search engines will not index it, and only users having a direct link can access it.

使用实体服务 API 创建组件和动态区域

🌐 Creating components and dynamic zones with the Entity Service API

Caution

在 Strapi v5 中,实体服务 API 已被弃用。请考虑改用 文档服务 API

🌐 The Entity Service API is deprecated in Strapi v5. Please consider using the Document Service API instead.

实体服务 是处理 组件动态区域 逻辑的层。通过实体服务 API,可以在创建或更新条目时 创建更新 组件和动态区域。

🌐 The Entity Service is the layer that handles components and dynamic zones logic. With the Entity Service API, components and dynamic zones can be created and updated while creating or updating entries.

创造

🌐 Creation

在使用实体服务 API 创建条目时,可以创建一个 组件

🌐 A component can be created while creating an entry with the Entity Service API:

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

在使用实体服务 API 创建条目时,可以创建一个动态区域(即组件列表):

🌐 A dynamic zone (i.e. a list of components) can be created while creating an entry with the Entity Service API:

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

更新

🌐 Update

在使用实体服务 API 更新条目时,可以更新一个 组件。如果指定了组件 id,则组件会被更新,否则旧的组件会被删除并创建一个新的组件:

🌐 A component 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:

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 (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:

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',
},
],
},
});