【发布时间】:2019-11-02 15:06:42
【问题描述】:
我正在尝试构建一个项目,其中有一个我必须在我的主文件中解析的 json 文件。但我不能将它包含在主文件中。在终端中,main.ts 和 main.js 都没有错误。 Webview 面板显示来自 html 的内容,但没有显示主文件中的内容。如果我通过开发人员工具进行检查,则会显示错误。我在 main.ts 中导入 json,而 main.js 是 main.ts 的编译文件。我需要这两个文件,并且其中任何一个都发生错误。
我尝试了不同的组合
组合一:
import json from "./test.json"; //in main.ts file
"module": "commonjs" // in tsconfig.json file
错误是“未在 main.js 文件中定义导出”
组合2:
const json = require("./test.json"); //in main.ts file
"module": "commonjs" // in tsconfig.json file
错误是“要求未在 main.ts 中定义”
组合3:
const json = require("./test.json"); //in main.ts file
"module": "es2015" // in tsconfig.json file
错误是“要求未在 main.ts 中定义”
组合4:
import json from "./test.json"; //in main.ts file
"module": "es2015" // in tsconfig.json file
错误是“不能在模块外使用 import 语句”
下面是我完整的 tsconfig.json 的示例
{
"compilerOptions": {
"module": "es2015",
"target": "es5",
"outDir": "out",
"sourceMap": true,
"strict": true,
"rootDir": "src",
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true,
"strictNullChecks":false,
"lib": ["dom","es2015","es5","es6"]
},
"exclude": ["node_modules", ".vscode-test"],
"include" : [
"src/**/*"
]
}
我做错了什么?请帮忙。提前致谢。
【问题讨论】:
-
所以你无法在 js 文件中导入 json 文件是什么问题?
-
是的。我在ts文件中导入json,js文件是编译后的文件。我需要这两个文件,并且其中任何一个都发生错误。
-
是的,我也试过了
-
打字稿的版本是多少?
标签: javascript json typescript webview vscode-extensions