简体   繁体   English

如何在 Javascript/React 中正确导入 JSON? (VS代码)

[英]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 导入路径,还可以为导入提供适当的智能感知。

用于 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+ 还会在所有不属于jsconfig resolveJsonModule

VS Code 1.44 中的默认行为

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.

相关问题 如何在 React、VS 代码中停止导入具有相对路径的节点模块 - How to stop import of node modules with relative paths in React, VS code 如何正确修改json in react? - How to properly modify json in react? 如何正确地导入和使用MSAL(用于js的Microsoft身份验证库)到javascript(非打字稿)反应单页应用程序中? - How to properly import and use the MSAL (Microsoft Authentication Library for js) into a javascript (non typescript) react single page application? JavaScript / React本机代码中的导入语句错误 - Error on import statement in JavaScript/React Native code 使用C#将JSON代码导入Javascript文件 - Import JSON code into Javascript file with C# 如何在 VS 代码中使用 tasks.json 正确延迟节点/Express API 的加载 - How to properly delay the loading of a Node/Express API using tasks.json in VS Code 如何在 VS Code 中的 React 项目中启用导入自动完成而不需要检查打字稿 - How to enable import autocomplete without typescript checking in a React project in VS Code 在 VS Code 中,如何在 React Node 应用程序中自动导入“断言”? - In VS Code How Can I Automatically Import "assert" in a React Node App? 如何在用 TypeScript 编写的 VS Code 扩展中导入 ES6 JavaScript 模块? - How do I import an ES6 JavaScript module in my VS Code extension written in TypeScript? 导入 React JS 的 Js 在 VS Code 中无法正确呈现 - Js with React JS imported is not rendering properly in VS Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM