简体   繁体   English

你如何为 React 配置 VSCode jsconfig?

[英]How do you configure VSCode jsconfig for React?

I am trying to make VSCode to work smoothly with my React project.我正在尝试使 VSCode 与我的 React 项目顺利运行。 I am refering to the ability to click into a component and go inside, peek at the component or propose stuff for auto completion (intellisense).我指的是单击组件和内部 go 的能力,查看组件或提出自动完成的内容(智能感知)。

I have this so far:到目前为止我有这个:

{
  "compilerOptions": {
      "module": "es6",
      "target": "es2019",
      "jsx": "react",
      "baseUrl": ".",
      "paths": {
        "@Reducers/*": ["./src/reducers/*"],
        "@Selectors/*": ["./src/selectors/*"],
        "@Components/*": ["./src/components/*"],
        "@App/*": ["./src/components/App/*"],
        "@Footer/*": ["./src/components/Footer/*"],
        "@Header/*": ["./src/components/Header/*"],
    }
  },
  "include" : ["src/**/*"],
  "exclude": ["node_modules", "dist", "config", ".vscode"]
}

I do not use CRA.我不使用 CRA。

Any idea why is not working?知道为什么不工作吗? Any help would be appreciated.任何帮助,将不胜感激。 Thank you in advance and regards预先感谢您和问候

edit: it seems that with module:commonjs instead of es6 is working better, any idea of why is this?编辑:似乎使用 module:commonjs 而不是 es6 效果更好,知道这是为什么吗? I am using es6 imports!我正在使用 es6 导入!

edit2: the reason is explained here: https://github.com/Microsoft/vscode/issues/24715 edit2:原因在这里解释: https://github.com/Microsoft/vscode/issues/24715

We need more information about you project.我们需要有关您的项目的更多信息。 but, All most problem about absolute path is base url.但是,关于绝对路径的所有最大问题都是基础 url。

If you used Webpack, Read Webpack - Alias .如果您使用 Webpack,请阅读Webpack - Alias and then read this too.然后也读这个。 Github CRA Path Issues Github CRA 路径问题

Examples例子

I used this.我用过这个。

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"],
    }
  }
}
const path = require('path');

// Webpack Config
export default {
  // ...
  resolve: {
    alias: {
      ['@']: path.join(root, 'src')
    }   
  }
}

If you have a root path, this is easy to change paths.如果您有根路径,则很容易更改路径。 So I recommend that don't use multiple '@' pahts too.所以我建议don't use multiple '@' pahts

Refs参考文献

The solution at the end was to use module: commonjs instead of module: es6最后的解决方案是使用 module: commonjs 而不是 module: es6

This is explained here: https://github.com/Microsoft/vscode/issues/24715这在这里解释: https://github.com/Microsoft/vscode/issues/24715

Quote: "In a jsconfig, the module option only changes how paths are resolved"引用:“在 jsconfig 中,模块选项只会改变路径的解析方式”

You need to install extensions for ReactJs like: ES7 React/Redux/GraphQL/React-Native snippets or Simple React Snippets .您需要为 ReactJs 安装扩展,例如: ES7 React/Redux/GraphQL/React-Native snippetsSimple React Snippets Try these two.试试这两个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在VSCode中,如何正确配置jsconfig.json以便使用index.js导入的绝对路径起作用? - In VSCode how to properly configure jsconfig.json in order for absolute paths with index.js imports to work? 你如何在 vscode 中显示反应组件道具的预期值? - How do you show expected values of react component props in vscode? 你如何配置一个反应应用程序来使用 typescript? - How do you configure a react app to use typescript? 如何配置Webpack以清除生产缩小的React警告? - How do you configure Webpack to clear the React Warning for production minification? 如何配置 VSCode 以在 React 中自动完成图像路径? - How to configure VSCode to autocomplete image paths in React? 如何在 React Native 项目中使用 jsconfig.json - How to use jsconfig.json in React Native project 如何配置 Create React App 以使用 Netlify Lambda 函数 - How Do You Configure Create React App to Work with Netlify Lambda Functions react-router 返回一个页面 你如何配置历史记录? - react-router go back a page how do you configure history? 如何在不破坏它的情况下将 jsconfig.json 添加到现有的 vscode 项目中 - How to add jsconfig.json to existing vscode project w/o breaking it 如何将现有项目与 jsconfig 集成 - How to integrate exist project with jsconfig
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM