如何正确导出模块,以便vscode可以在建议[ctrl+space]中显示我的模块? 我有一个设置样式文件,例如: GlobalStyles.js 导出const globalstyles = { ViewContainer:{flex:1,JustifyContent:'Center',AlignItems:'Center'}, 中心:{JustifyContent:'Center',AlignItems:'

问题描述 投票:0回答:3

export const GlobalStyles={ ViewContainer:{flex:1, justifyContent:'center', alignItems:'center'}, Center:{justifyContent:'center', alignItems:'center'}, FontNormal:15, FontMedium:18, FontLarge:28, FontHeader:38 } module.export={GlobalStyles}

当我在另一个JS文件中使用它时,请说
Home.js

。我想VSCODE知道我定义的每一个,

Key:Value

vscode中导出的那些提示这样的导入样式:
GlobalStyles.js

我的预期结果是:

import { GlobalStyles } from '../Component/GlobalStyles';

如何让vscode建议我
auto tot from to/to/globalstyles
当我键入

foo

时?
import { ViewContainer, Center, FontMedium, [and so on]} from '../Component/GlobalStyles';
就像

foo 您正在谈论的功能称为

autoImports
。为了回答您的问题,此功能存在并默认情况下在Vscode中工作。
这里是我如何在React项目中使用它的示例。在这里,我在一个具有相同名称的文件夹中有一个组件。该文件夹之外是我称为moduleexports.js的文件,目前具有以下代码。

ViewContainer, Center, FontMedium, [and so on]

reactjs visual-studio-code intellisense
3个回答
4
投票

可以在不作为组件的情况下完成相同的操作。我将在同一文件moduleexports.js中声明一个testObject并导出它。

让我们看看Intellisense是否会捡起它。 example import suggestion 1

有。我希望这会有所帮助,并确实询问您是否想要更多澄清或陷入问题。 create a testObject 在这种情况下,错误似乎正在使用动态导出。那是旧的

commonjs

风格。 VSCODE使用打字稿工具进行静态分析。为了利用这一点,您必须使用

ES6Export

.。 Commonjs:it worksimport Navbar from "./Navbar/Navbar"; export { Navbar };

ES6-MODULES

0
投票
module.export={GlobalStyles}

两个之间的显着差异在于,commonjs变体只是在运行时定义的变量。 IE。没有执行代码的情况下,无法知道您要导出的内容。 es6export GlobalStyles是保留的单词。它的构造是不能在定义下更改的,这也意味着可以找到它的类型定义,而无需实际执行代码。

typecript,babel等提供两个模块系统之间的互操作性,但它们是两个截然不同的东西。

 -hi这是一个代码示例,该示例具有正确的语法,用于在vscode

中的JS中导出和导入函数

ExportModule.js(文件名) 代码:

export

ImportModule.js(文件名)

代码:

function testExportFunc(){ console.log("test export worked") }; function testExportFunc2(){ console.log("test export 2 also worked") }; export default { testExportFunc,testExportFunc2 };


0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.