主页自定义
¥Homepage customization
5.13.0+Adding custom widgets is available since Strapi 5.13.0. The newest widgets (profile, entries, statistics) are available from Strapi 5.23.0.Page summary:
The admin panel homepage displays default content and profile widgets and supports custom additions through the
app.widgets.registerAPI.
主页是 Strapi 管理面板的登录页面。默认情况下,它使用 5 个默认小部件提供内容概览:
¥The Homepage is the landing page of the Strapi admin panel. By default, it provides an overview of your content with 5 default widgets:
-
上次编辑的条目:显示最近修改的内容条目,包括其内容类型、状态和更新时间。
¥Last edited entries: Displays recently modified content entries, including their content type, status, and when they were updated.
-
上次发布的条目:显示最近发布的内容条目,让你快速访问和管理已发布的内容。
¥Last published entries: Shows recently published content entries, allowing you to quickly access and manage your published content.
-
简介:显示你的个人资料的简短摘要,包括你的名称、电子邮件地址和角色。
¥Profile: Displays a short summary of your profile, including your name, email address, and role.
-
条目:显示草稿和已发布条目的总数。
¥Entries: Displays the total number of Draft & Published entries.
-
项目统计:显示有关你的条目、内容类型、语言环境、资源等的统计信息。
¥Project statistics: Displays statistics about your entries, content-types, locales, assets, and more.

这些默认小部件目前无法删除,但你可以通过创建自己的小部件来自定义主页。
¥These default widgets cannot currently be removed, but you can customize the Homepage by creating your own widgets.
如果你最近创建了一个 Strapi 项目,主页可能还会在小部件上方显示导览(如果你尚未跳过它)(有关详细信息,请参阅 管理面板 文档)。
¥If you recently created a Strapi project, the Homepage may also display a guided tour above widgets if you haven't skipped it yet (see Admin Panel documentation for details).
添加自定义小部件
¥Adding custom widgets
要添加自定义小部件,你可以:
¥To add a custom widget, you can:
-
从 市场 安装插件
¥install a plugin from the Marketplace
-
或者,创建并注册你自己的小部件
¥or create and register your own widgets
本页面将介绍如何创建和注册小部件。
¥The present page will describe how to create and register your widgets.
注册自定义小部件
¥Registering custom widgets
要注册小部件,请使用 app.widgets.register():
¥To register a widget, use app.widgets.register():
-
如果你正在构建插件,请在
index文件的插件register生命周期方法 中更新(推荐方法);¥in the plugin’s
registerlifecycle method of theindexfile if you're building a plugin (recommended way), -
或者,如果你只将小部件添加到一个没有插件的 Strapi 应用中,请在 应用的全局
register()生命周期方法 中更新。¥or in the application's global
register()lifecycle method if you're adding the widget to just one Strapi application without a plugin.
本页上的示例将介绍如何通过插件注册窗口小部件。如果你在应用的全局 register() 生命周期方法中注册了小部件,则大部分代码应该可重用,但不应传递 pluginId 属性。
¥The examples on the present page will cover registering a widget through a plugin. Most of the code should be reusable if you register the widget in the application's global register() lifecycle method, except you should not pass the pluginId property.
- JavaScript
- TypeScript
import pluginId from './pluginId';
import MyWidgetIcon from './components/MyWidgetIcon';
export default {
register(app) {
// Register the plugin itself
app.registerPlugin({
id: pluginId,
name: 'My Plugin',
});
// Register a widget for the Homepage
app.widgets.register({
icon: MyWidgetIcon,
title: {
id: `${pluginId}.widget.title`,
defaultMessage: 'My Widget',
},
component: async () => {
const component = await import('./components/MyWidget');
return component.default;
},
/**
* Use this instead if you used a named export for your component
*/
// component: async () => {
// const { Component } = await import('./components/MyWidget');
// return Component;
// },
id: 'my-custom-widget',
pluginId: pluginId,
});
},
bootstrap() {},
// ...
};
import pluginId from './pluginId';
import MyWidgetIcon from './components/MyWidgetIcon';
import type { StrapiApp } from '@strapi/admin/strapi-admin';
export default {
register(app: StrapiApp) {
// Register the plugin itself
app.registerPlugin({
id: pluginId,
name: 'My Plugin',
});
// Register a widget for the Homepage
app.widgets.register({
icon: MyWidgetIcon,
title: {
id: `${pluginId}.widget.title`,
defaultMessage: 'My Widget',
},
component: async () => {
const component = await import('./components/MyWidget');
return component.default;
},
/**
* Use this instead if you used a named export for your component
*/
// component: async () => {
// const { Component } = await import('./components/MyWidget');
// return Component;
// },
id: 'my-custom-widget',
pluginId: pluginId,
});
},
bootstrap() {},
// ...
};
app.widgets.register API 仅适用于 Strapi 5.13 及更高版本。尝试使用旧版本的 Strapi 调用 API 将导致管理面板崩溃。想要注册小部件的插件开发者应该:
¥The app.widgets.register API only works with Strapi 5.13 and above. Trying to call the API with older versions of Strapi will crash the admin panel.
Plugin developers who want to register widgets should either:
-
在其插件
package.json中将^5.13.0设置为其@strapi/strapipeerDependency。此对等依赖支持 Marketplace 的兼容性检查。¥set
^5.13.0as their@strapi/strapipeerDependency in their pluginpackage.json. This peer dependency powers the Marketplace's compatibility check. -
或者,在调用 API 之前检查它是否存在:
¥or check if the API exists before calling it:
if ('widgets' in app) {
// proceed with the registration
}
如果插件的主要目的是注册小部件,则建议使用 peerDependency 方法。如果插件想要添加小部件,但其大部分功能位于其他位置,则第二种方法更有意义。
¥The peerDependency approach is recommended if the whole purpose of the plugin is to register widgets. The second approach makes more sense if a plugin wants to add a widget but most of its functionality is elsewhere.
小部件 API 参考
¥Widget API reference
app.widgets.register() 方法可以接受单个窗口小部件配置对象或配置对象数组。每个小部件配置对象都可以接受以下属性:
¥The app.widgets.register() method can take either a single widget configuration object or an array of configuration objects. Each widget configuration object can accept the following properties:
| 属性 | 类型 | 描述 | 必需的 |
|---|---|---|---|
icon | React.ComponentType | 显示在小部件标题旁边的图标组件 | 是的 |
title | MessageDescriptor | 支持翻译的小部件标题 | 是的 |
component | () => Promise<React.ComponentType> | 返回小部件组件的异步函数 | 是的 |
id | string | 小部件的唯一标识符 | 是的 |
link | Object | 可选链接,可添加到小部件(参见链接对象属性) | 不 |
pluginId | string | 注册小部件的插件 ID | 不 |
permissions | Permission[] | 查看小部件所需的权限 | 不 |
链接对象属性:
¥Link object properties:
如果你想在小部件上添加链接(例如,导航到详细视图),你可以提供一个具有以下属性的 link 对象:
¥If you want to add a link to your widget (e.g., to navigate to a detailed view), you can provide a link object with the following properties:
| 属性 | 类型 | 描述 | 必需的 |
|---|---|---|---|
label | MessageDescriptor |