[英]How to import JSON in Javascript/React properly? (VS Code)
I have this example file (example.json):我有这个示例文件(example.json):
{"test": {"name": "testName"}}
Then, in my js file I import like this:然后,在我的 js 文件中,我像这样导入:
import testJson from "./example.json"
When I type: testJson.
当我输入:
testJson.
I want suggestions to appear in the IDE (preferably in VS Code).我希望建议出现在 IDE 中(最好在 VS Code 中)。
How can I accomplish this?我怎样才能做到这一点?
Use Node JSON Autocomplete it will add autocomplete in vscode.使用节点 JSON 自动完成,它将在 vscode 中添加自动完成。
You don't need any extensions for this.您不需要任何扩展。
JSON imports are not supported in every module system so you need to tell VS Code that such imports are valid.并非每个模块系统都支持 JSON 导入,因此您需要告诉 VS Code 这样的导入是有效的。 To do this, in your jsconfig file, just add:
为此,在您的jsconfig文件中,只需添加:
"resolveJsonModule": true
This will not only let you auto complete json import paths, but also give you proper intellisense for the import.这不仅可以让您自动完成 json 导入路径,还可以为导入提供适当的智能感知。
VS Code 1.44+ also automatically enables resolveJsonModule
in all JS files that do not belong to a jsconfig
: VS Code 1.44+ 还会在所有不属于
jsconfig
resolveJsonModule
Again though, if you have a jsconfig or tsconfig you'll need to explicitly set "resolveJsonModule": true
不过,如果你有一个 jsconfig 或 tsconfig,你需要显式设置
"resolveJsonModule": true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.