【问题标题】:Critical dependency: the request of a dependency is an expression while using lazy loading关键依赖:依赖的请求是使用延迟加载时的表达式
【发布时间】:2022-08-03 22:37:55
【问题描述】:

我正在尝试动态导入模块但出现以下错误 -

编译有问题:X

./src/.../useCustomModule.js 21:21-56 中的警告

关键依赖:依赖的请求是一个表达式

import React from \"react\";
import PropTypes from \"prop-types\";

export const moduleMapping = {
    CONTEXT_ONE: \"./....contextOnePath\",
    CONTEXT_TWO: \"./....contextTwoPath\",
    
};

const getModule = (moduleName) => {
    const module = React.lazy(() => import(moduleMapping[moduleName]));
    return module;
};

export const useCustomModule = (moduleName) => {
    return getModule(moduleName);
};

注意:在 eslintrc.json 我有以下设置 - \"ecmaVersion\": 12,

    标签: javascript reactjs lazy-loading


    【解决方案1】:

    最近,我也遇到了类似的问题,但是当我使用字符串插值时,警告消失了。 在您的代码中,尝试一下:

    const getModule = (moduleName) => {
        const module = React.lazy(() => import(`${moduleMapping[moduleName])}`);
        return module;
    };
    

    【讨论】:

      【解决方案2】:
      const getModule = (moduleName) => {
      const module = React.lazy(() => import(`${moduleMapping[moduleName])}`);
      return module;
      

      };

      【讨论】:

        猜你喜欢
        • 2016-08-28
        • 2017-08-11
        • 2019-05-04
        • 2021-06-13
        • 2020-06-15
        • 2020-10-11
        • 1970-01-01
        • 2022-11-10
        • 2022-08-15
        相关资源
        最近更新 更多