我想在我的文件中使用import,但我无法找到正确替换我的需求的方法
查看我要替换的代码
const object = {
first: require('../example/first.json').EXL.PUBLIC,
second: require('../example/second.json').EXL.PUBLIC,
third: require('../example/third.json').EXL.PUBLIC
}
第一个问题是如何将这些东西直接导入对象?就像我对要求一样?
第二个,如何使用.EXL.PUBLIC
命令导入?要直接导入json文件的右分支?
第一个问题是如何将这些东西直接导入对象?
你不能,你必须导入它们然后将它们添加到对象。
第二个,如何使用'.EXL.PUBLIC'命令导入?
您必须导入该项目,然后提取该属性。
我假设您正在使用Node.js:
在.mjs
模块中,您可以这样做:
import firstRoot from "../example/first.json";
import secondRoot from "../example/second.json";
import thirdRoot from "../example/third.json";
const object = {
first: firstRoot.EXL.PUBLIC,
second: secondRoot.EXL.PUBLIC,
third: thirdRoot.EXL.PUBLIC
};
您仍然可以像在v11中那样执行此操作。
如果您通过.js
中的新"type": "module"
将ESM与package.json
文件一起使用,则需要添加--experimental-json-modules
标志以启用JSON加载。更多关于v12的支持here,但请注意--type
尚不支持(如果是,可能是--entry-type
),JSON标志是--experimental-json-modules
,而不是--experimental-json-loader
)。