简体   繁体   English

带有 JavaScript 的 React Native 的 VSCode 自动导入

[英]VSCode auto imports for React Native with JavaScript

I know VSCode supports auto import for JavaScript.我知道 VSCode 支持 JavaScript 的自动导入。

For some reason, it does not work with React Native.由于某种原因,它不适用于 React Native。 There is even a dedicated section on the VSCode website for this. VSCode 网站上什至有一个专门的部分 But it only answers how to fix this problem for Typescript (using allowSyntheticDefaultImports ).但它只回答了如何解决 Typescript 的这个问题(使用allowSyntheticDefaultImports )。 I want it for JavaScript (ES6).我想要它用于 JavaScript (ES6)。

I tried explicitly setting "javascript.suggest.autoImports": true, in my settings.json but that wouldn't change anything, since true is the default anyways.我尝试在我的settings.json中明确设置"javascript.suggest.autoImports": true, true不会改变任何东西,因为无论如何都是默认设置。

How to get auto imports for React Native with JavaScript in VSCode ?如何在VSCode中使用JavaScriptReact Native自动导入?

PS: This plugin (suggested in this similar question ) is also only for TypeScript. PS: 这个插件(在这个类似的问题中建议)也仅适用于 TypeScript。 Besides that, since VSCode includes auto import out of the box there should be a solution without an extension for RN and JS.除此之外,由于 VSCode 包括开箱即用的自动导入,因此应该有一个解决方案,无需扩展RN 和 JS。

You might have the 'Auto Imports' option disabled.您可能禁用了“自动导入”选项。
In VSCode go to Settings and search for 'Auto Imports' option, it should be something like this:在 VSCode 中,转到“设置”并搜索“自动导入”选项,它应该是这样的:

在此处输入图片说明

Just activate it if it's disabled and you'll be fine.如果它被禁用,只需激活它,你会没事的。

  1. Install Auto Imports VSC extension, and enable Auto-imports options(for JS) in the VSC settings (File -> Preferences -> Settings).安装Auto Imports VSC 扩展,并在 VSC 设置(文件 -> 首选项 -> 设置)中启用自动导入选项(适用于 JS)。

  2. You may try ctrl+space (for example, after placing the cursor inside the <Text> element) and select the import element from the displayed options.您可以尝试ctrl+space (例如,将 cursor 放在<Text>元素内)和 select 显示选项中的导入元素。

For me the issue was neither the plug in nor VSCode.对我来说,问题既不是插件也不是 VSCode。

I tried selecting suggestions with library name besides it and it worked for me.我尝试选择带有库名称的建议,它对我有用。 ie if I want component then I will select Componet react from the suggestion and it worked.即,如果我想要组件,那么我将 select Componet 对建议做出反应并且它起作用了。

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

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