【发布时间】:2020-04-25 07:03:36
【问题描述】:
我正在尝试使用 Webpack 文件加载器将静态 json 资源文件复制到 dist/,以便我可以将该 json 文件导入 Vue.js 组件中。当我运行npm run build 时,Webpack 正在正确复制文件,但是当我尝试导入文件(如import myJson from '@/static/runtimeConfig.json')时,“myJSON”变量只获取文件路径而不是实际的 JSON 文件内容。谁能帮我理解这是为什么?
我的 webpack 文件加载器配置如下所示:
module: {
//load runtimeConfig.json file through a custom loader
rules: [
{
// set the type to javascript/auto to bypass webpack's built-in json importing
type: 'javascript/auto',
test: /(runtimeConfig\.json)$/i,
// file-loader resolves imports on a file and emits that file to dist/
loader: 'file-loader',
// use filename runtimeConfig.json instead of a hashed filename
options: {
name: '[path][name].[ext]',
outputPath: 'static',
}
}
],
},
我的意图是在 vue.js 组件中导入一个 json 文件,如下所示:
import myJson from '@/static/runtimeConfig.json'
但是,在我进行导入之后,myJson 只是文件的路径,而不是实际的 json 内容
console.log(myJson)
--> /static/src/static/runtimeConfig.json
【问题讨论】: