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

提醒

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

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

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

¥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 创建条目时可以创建 component

¥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 更新条目时更新 component。如果指定了组件 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',
},
],
},
});