Skip to main content

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.

🤓 v3/v4 比较

如果手动安装或在 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:

要在 v4 中启用本地插件并定义可选配置:

¥To enable a local plugin in v4 and define an optional configuration:

  1. 如果该文件尚不存在,请创建 ./config/plugins.js 文件。

    ¥If it does not already exist, create the ./config/plugins.js file.

  2. ./config/plugins.js 文件中,导出一个函数:

    ¥In the ./config/plugins.js file, export a function that:

    • 返回一个对象

      ¥returns an object

    • 并且可以将 { env } 对象作为参数(参见 环境配置 文档)。

      ¥and can take the { env } object as a parameter (see Environment configuration documentation).

  3. ./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.

  4. "my-plugin" 对象中,将 enabled 键值设置为 true(布尔值)。

    ¥Within the "my-plugin" object, set the enabled key value to true (boolean).

  5. (可选)添加 resolve 键,其值是插件文件夹的路径(作为字符串),以及 config 键(作为对象),可以包含插件的其他配置(请参阅 插件配置 文档)。

    ¥(optional) Add a resolve key, whose value is the path of the plugin folder (as a string), and a config key (as an object) that can include additional configuration for the plugin (see plugins configuration documentation).

Example: Enabling and configuring a "my-plugin" plugin
./config/plugins.js

module.exports = ({ env }) => ({
"my-plugin": {
enabled: true,
resolve: "./path-to-my-plugin",
config: {
// additional configuration goes here
},
},
});
✏️ 插件发布在 npm 上

如果插件将在 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" } }).