当我将字符串传递给计算属性中的文件加载器 require 方法时,它工作正常,如下所示
computedProp () {
return require('../path/to/file');
}
但是如果我尝试将一些变量传递给它,它会抛出错误
computedProp () {
const l = '../path/to/file';
return require(l);
}
错误:错误:找不到模块“。”
我该如何解决这个问题?我想根据某些条件创建相对路径,然后将其传递给 require 方法来获取绝对路径。
最佳答案
非常相似的答案已发布在 Stack 中:
Since Webpack is running in build-time, it can't figure out which modules to bundle when the name is a dynamic variable. You can give it hints by specifying part of the path (for example, if you know all the modules are in a single directory).
Using require('...') with a variable vs. using a string in webpack
因此,为了使其发挥作用,您可以测试:
computedProp () {
const path = '../path/to'
const file = 'file';
return require(path + '/' + file);
}
希望有帮助。
关于webpack - Vuejs : Cannot find module ".",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44673258/