首页 > 解决方案 > 在 Chrome 控制台中加载外部库

问题描述

我正在使用 React 和 Redux。在我的减速器中,我正在使用lodash库。

debugger; 命令停止执行

如果我导入lodash

import _ from "../../node_modules/lodash";

或者

import _ from "lodash";

在此处输入图像描述

它失败。它无法导入库。运行我的 React 应用程序时没有加载错误。lodash我想知道您如何在 Google Chrome 控制台中加载外部库(即)?

标签: javascriptreactjsdebuggingreact-redux

解决方案


改用动态导入语法,你会得到一个你想要的 Promise。例如,在此页面上:

https://stackoverflow.com/questions/57032914/loading-external-library-in-chrome-console

使用

import('../../foo')

结果是

在此处输入图像描述

假设在您自己的网站上,链接是正确的,您需要做的就是调用.thenPromise:

import("../../node_modules/lodash")
  .then((_) => {
    // do stuff with _
  });

(当然,这要求是node_modules当前页面的祖父目录的子文件夹)


推荐阅读