v4 插件迁移:启用插件
¥v4 plugin migration: Enabling a plugin
本指南是 v4 插件迁移指南 的一部分,旨在帮助你将插件从 Strapi v3.6.x 迁移到 v4.0.x。
¥This guide is part of the v4 plugin migration guide designed to help you migrate a plugin from Strapi v3.6.x to v4.0.x.
如果手动安装或在 plugins 目录中找到 Strapi v3 插件,则该插件已启用。
¥A Strapi v3 plugin was enabled if it was manually installed or found in the plugins directory.
在 Strapi v4 中:
¥In Strapi v4:
遵循 自动插件发现 模式安装的插件将自动启用。
¥Installed plugins following the automatic plugins discovery pattern will automatically be enabled.
开发本地插件时,必须在 Strapi 应用的
./config/plugins.js文件 中显式启用该插件。¥While developing a local plugin, the plugin must explicitly be enabled in the
./config/plugins.jsfile of the Strapi application.
要在 v4 中启用本地插件并定义可选配置:
¥To enable a local plugin in v4 and define an optional configuration:
如果该文件尚不存在,请创建
./config/plugins.js文件。¥If it does not already exist, create the
./config/plugins.jsfile.在
./config/plugins.js文件中,导出一个函数:¥In the
./config/plugins.jsfile, export a function that:返回一个对象
¥returns an object
并且可以将
{ env }对象作为参数(参见 环境配置 文档)。¥and can take the
{ env }object as a parameter (see Environment configuration documentation).
在
./config/plugins.js导出的对象中,使用插件名称创建一个键(例如"my-plugin")。这个键的值也是一个对象。¥Within the object exported by
./config/plugins.js, create a key with the name of the plugin (e.g."my-plugin"). The value of this key is also an object.在
"my-plugin"对象中,将enabled键值设置为true(布尔值)。¥Within the
"my-plugin"object, set theenabledkey value totrue(boolean).(可选)添加
resolve键,其值是插件文件夹的路径(作为字符串),以及config键(作为对象),可以包含插件的其他配置(请参阅 插件配置 文档)。¥(optional) Add a
resolvekey, whose value is the path of the plugin folder (as a string), and aconfigkey (as an object) that can include additional configuration for the plugin (see plugins configuration documentation).
Example: Enabling and configuring a "my-plugin" plugin
module.exports = ({ env }) => ({
"my-plugin": {
enabled: true,
resolve: "./path-to-my-plugin",
config: {
// additional configuration goes here
},
},
});
如果插件将在 npm 上发布,则 package.json 文件应包含 strapi.kind 键,其值设置为 "plugin"(即 { "strapi": { "kind": "plugin" } })。
¥If the plugin will be published on npm, the package.json file should include a strapi.kind key with a value set to "plugin" (i.e. { "strapi": { "kind": "plugin" } }).