如何为 Strapi 插件创建组件
¥How to create components for Strapi plugins
当 开发 Strapi 插件 时,你可能希望为你的插件创建可重用的组件。Strapi 中的组件是可重复使用的数据结构,可用于不同的内容类型。
¥When developing a Strapi plugin, you might want to create reusable components for your plugin. Components in Strapi are reusable data structures that can be used across different content-types.
要为 Strapi 插件创建组件,你需要遵循与创建内容类型类似的方法,但有一些具体的区别。
¥To create components for your Strapi plugin, you'll need to follow a similar approach to creating content-types, but with some specific differences.
创建组件
¥Creating components
你可以通过两种不同的方式为你的插件创建组件:使用内容类型生成器(推荐方式)或手动。
¥You can create components for your plugins in 2 different ways: using the Content-Type Builder (recommended way) or manually.
使用内容类型构建器
¥Using the Content-Type Builder
建议通过管理面板中的 Content-Type Builder 为插件创建组件。内容类型生成器文档 提供了有关此过程的更多详细信息。
¥The recommended way to create components for your plugin is through the Content-Type Builder in the admin panel. The Content-Type Builder documentation provides more details on this process.
手动创建组件
¥Creating components manually
如果你更喜欢手动创建组件,则需要:
¥If you prefer to create components manually, you'll need to:
-
在插件结构中创建组件模式。
¥Create a component schema in your plugin's structure.
-
确保组件已正确注册。
¥Make sure the component is properly registered.
插件的组件应放在插件结构中的相应目录中。你通常会在插件的服务器部分中创建它们(参见 插件结构文档)。
¥Components for plugins should be placed in the appropriate directory within your plugin structure. You would typically create them within the server part of your plugin (see plugin structure documentation).
有关 Strapi 中组件的更多详细信息,你可以参考 模型属性文档。
¥For more detailed information about components in Strapi, you can refer to the Model attributes documentation.
查看组件结构
¥Reviewing the component structure
Strapi 中的组件在定义中遵循以下格式:
¥Components in Strapi follow the following format in their definition:
{
"attributes": {
"myComponent": {
"type": "component",
"repeatable": true,
"component": "category.componentName"
}
}
}
使组件在管理面板中可见
¥Making components visible in the admin panel
为了确保你的插件组件在管理面板中可见,你需要在组件架构中设置适当的 pluginOptions
:
¥To ensure your plugin's components are visible in the admin panel, you need to set the appropriate pluginOptions
in your component schema:
{
"kind": "collectionType",
"collectionName": "my_plugin_components",
"info": {
"singularName": "my-plugin-component",
"pluralName": "my-plugin-components",
"displayName": "My Plugin Component"
},
"pluginOptions": {
"content-manager": {
"visible": true
},
"content-type-builder": {
"visible": true
}
},
"attributes": {
"name": {
"type": "string"
}
}
}
此配置确保你的组件在内容类型构建器和内容管理器中都可见且可编辑。
¥This configuration ensures your components will be visible and editable in both the Content-Type Builder and Content Manager.