【问题标题】:Uncaught TypeError: __webpack_require__(…).context is not a function未捕获的类型错误:__webpack_require__(...).context 不是函数
【发布时间】:2019-10-31 20:20:14
【问题描述】:

当我尝试使用此代码动态导入 vue 组件时:

const components = require.context('./', true, '/^index\.js$/');

我收到此错误:

app.js:9 Uncaught TypeError: __webpack_require__(...).context is not a function
    at Module../asset/app.js (app.js:9)
    at __webpack_require__ (bootstrap:782)
    at fn (bootstrap:150)
    at Object.0 (app.293d5fe1d8a073fed37a.bundle.js:1747)
    at __webpack_require__ (bootstrap:782)
    at checkDeferredModules (bootstrap:45)
    at bootstrap:858
    at bootstrap:858

这是为什么呢?如何解决?我错过了什么?

【问题讨论】:

    标签: webpack webpack-4


    【解决方案1】:

    doc link

    文档中有一句话The arguments passed to require.context must be literals。 第三个参数应该是像/^index.js$/ 这样的文字。我在使用new RegExp()时遇到了同样的错误

    【讨论】:

      【解决方案2】:

      尝试将第三个参数从字符串更改为正则表达式,即:

      const components = require.context('./', true, /^index.js$/)
      

      我有一些使用正则表达式的用法,以及我正在尝试整理的其他一些用法,它们不使用像这样的内联正则表达式。

      旁注:您要求从 './' 向下查找所有内容(递归:您将该标志设置为 true),但只会接受一个文件:index.js 在基本文件夹中。如果这就是你想要的,我建议将递归标志更改为 false - 它会更快。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-23
        • 1970-01-01
        • 1970-01-01
        • 2021-09-24
        • 2022-11-19
        • 1970-01-01
        • 2019-06-06
        • 2019-05-24
        相关资源
        最近更新 更多