插件的管理面板 API 概述
🌐 Admin Panel API for plugins: An overview
Page summary:
管理面板 API 提供
register、bootstrap和registerTrads钩子,用于向 Strapi 的 UI 注入 React 组件和翻译。菜单、设置、注入区、reducer 和钩子 API 允许插件添加导航、配置面板或自定义操作。
Strapi 插件可以与 Strapi 应用的后端和前端进行交互。管理面板 API 涵盖了前端部分:它允许插件自定义 Strapi 的管理面板。管理面板是一个 React 应用,每个插件都会为其贡献自己的 React 应用。自定义包括编辑入口文件以导出所需的界面,并选择要执行的操作。
有关插件如何与 Strapi 的后端部分交互的更多信息,请参见 服务器 API。
🌐 For more information on how plugins can interact with the back end part of Strapi, see Server API.
在深入了解本页的概念之前,请确保你已经创建了一个 Strapi 插件。
🌐 Before diving deeper into the concepts on this page, please ensure you created a Strapi plugin.
入口文件
🌐 Entry file
管理面板 API 的入口文件是 [plugin-name]/admin/src/index.js。该文件导出所需的接口,并提供以下功能:
🌐 The entry file for the Admin Panel API is [plugin-name]/admin/src/index.js. This file exports the required interface, with the following functions available:
| 功能类型 | 可用功能 || --- | --- || 生命周期函数 | register(), bootstrap() || 异步函数 | registerTrads()(详情见 管理员本地化) |
所有管理面板代码技术上可以全部放在单个入口文件中,但强烈建议将每个关注点拆分到各自的文件夹中,就像 strapi generate plugin CLI 命令生成的那样。本文件中的示例遵循这种结构。
🌐 All admin panel code can technically live in the single entry file, but splitting each concern into its own folder, as generated by the strapi generate plugin CLI command, is strongly recommended. The examples in this documentation follow that structure.
register()
类型: Function
此函数在加载插件时被调用,甚至在应用实际上被 引导 之前。它将正在运行的 Strapi 应用作为参数 (app)。
🌐 This function is called to load the plugin, even before the app is actually bootstrapped. It takes the running Strapi application as an argument (app).
在 register() 函数中,插件可以:
🌐 Within the register() function, a plugin can:
- 使用
registerPlugin()注册自身,使其可以在管理面板中使用 - 在主导航中添加一个新链接(见 管理员导航与设置)
- 创建一个新的设置部分
- 定义injection zones
- 添加 reducers
示例:
- JavaScript
- TypeScript
import pluginId from './pluginId';
export default {
register(app) {
app.registerPlugin({ id: pluginId, name: 'My Plugin' });
// Add menu links, settings sections, injection zones, and reducers here
},
};
import type { StrapiApp } from '@strapi/admin/strapi-admin';
import pluginId from './pluginId';
export default {
register(app: StrapiApp) {
app.registerPlugin({ id: pluginId, name: 'My Plugin' });
// Add menu links, settings sections, injection zones, and reducers here
},
};
registerPlugin()
类型: Function
注册插件以使其在管理面板中可用。此函数在register()生命周期函数中被调用,并返回一个包含以下参数的对象:
🌐 Registers the plugin to make it available in the admin panel. This function is called within the register() lifecycle function and returns an object with the following parameters:
| 参数 | 类型 | 描述 || --- | --- | --- || id | 字符串 | 插件 ID || name | 字符串 | 插件名称 || apis | Record<string, unknown> | 向其他插件开放的 API || initializer | React.ComponentType | 插件初始化的组件 || injectionZones | 对象 | 可用 注入区域 的声明 || isReady | 布尔值 | 插件就绪状态(默认值:true) |
一些参数可以从 package.json 文件中导入。
🌐 Some parameters can be imported from the package.json file.
示例:
- JavaScript
- TypeScript
export default {
register(app) {
app.registerPlugin({
id: 'my-plugin',
name: 'My Plugin',
apis: {
// APIs exposed to other plugins
},
initializer: MyInitializerComponent,
injectionZones: {
// Areas where other plugins can inject components
},
isReady: false,
});
},
};
import type { StrapiApp } from '@strapi/admin/strapi-admin';
export default {
register(app: StrapiApp) {
app.registerPlugin({
id: 'my-plugin',
name: 'My Plugin',
apis: {
// APIs exposed to other plugins
},
initializer: MyInitializerComponent,
injectionZones: {
// Areas where other plugins can inject components
},
isReady: false,
});
},
};