简体   繁体   中英

How to import JSON in Javascript/React properly? (VS Code)

I have this example file (example.json):

{"test": {"name": "testName"}}

Then, in my js file I import like this:

import testJson from "./example.json"

When I type: testJson.

I want suggestions to appear in the IDE (preferably in VS Code).

How can I accomplish this?

Use Node JSON Autocomplete it will add autocomplete in 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. To do this, in your jsconfig file, just add:

"resolveJsonModule": true

This will not only let you auto complete json import paths, but also give you proper intellisense for the import.

用于 package.json 的 IntelliSense

VS Code 1.44+ also automatically enables resolveJsonModule in all JS files that do not belong to a jsconfig :

VS Code 1.44 中的默认行为

Again though, if you have a jsconfig or tsconfig you'll need to explicitly set "resolveJsonModule": true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM