Skip to main content

Strapi 工厂导入已更新

¥Strapi factories import have been updated

在 Strapi 5 中,strapi 工厂导入已更新。

¥In Strapi 5, strapi factories import have been updated.

此页面是 重大变更数据库 的一部分,提供有关重大更改的信息以及从 Strapi v4 迁移到 Strapi 5 的其他说明。

¥This page is part of the breaking changes database and provides information about the breaking change and additional instructions to migrate from Strapi v4 to Strapi 5.

\🔌 此重大更改是否会影响插件?\
\🤖 此重大更改是否由 codemod 自动处理?\部分

重大更改描述

¥Breaking change description

在 Strapi v4 中

¥In Strapi v4

导入操作如下:

¥Imports are done as follows:

  • 使用应用初始化函数:

    ¥using the application init function:

    import strapi from '@strapi/strapi'; 
    // or
    const strapi = require('@strapi/strapi');

    strapi();
  • 使用工厂:

    ¥using the factories:

    import strapiDefault from '@strapi/strapi';
    // or
    import { factories } from '@strapi/strapi';
    // or
    const { factories } = require('@strapi/strapi');
    // or
    const { createCoreService } = require('@strapi/strapi').factories;
    // or
    const strapi = require('@strapi/strapi');

    strapiDefault.factories.createCoreService(); // this is not possible anymore in v5
    strapi.factories.createCoreService();
    factories.createCoreService();
    createCoreService();

在 Strapi 5 中

¥In Strapi 5

导入操作如下:

¥Imports are done as follows:

  • 使用应用初始化函数

    ¥using the application init function

    import { createStrapi } from '@strapi/strapi'; 
    const { createStrapi } = require('@strapi/strapi');
    const strapi = require('@strapi/strapi');

    strapi.createStrapi();
  • 使用工厂:

    ¥using the factories:

    // Using the factories
    import { factories } from '@strapi/strapi';
    // or
    const { factories } = require('@strapi/strapi');
    // or
    const { createCoreService } = require('@strapi/strapi').factories;
    // or
    const strapi = require('@strapi/strapi');

    strapi.factories.createCoreService();
    factories.createCoreService();
    createCoreService();

    // The recommended way is
    const { factories } = require('@strapi/strapi');
    import { factories } from '@strapi/strapi';

    factories.createCoreService();

迁移

¥Migration

  • 升级工具 使用 codemod 转换应用实例。

    ¥The upgrade tool converts the application instantiation with a codemod.

  • 已删除的工厂模式不会自动转换。

    ¥The factories pattern that were removed will not be converted automatically.