Javascript 从file.js中导入所有变量,并使用;导入&x27/js'&引用;召唤

Javascript 从file.js中导入所有变量,并使用;导入&x27/js'&引用;召唤,javascript,reactjs,Javascript,Reactjs,我正在使用导入功能,我希望使用类似于css的导入,我的意思是“import./file.css'”,然后所有css属性都扩散到文件中。我用ReactJS尝试过同样的方法,但失败了 我的期望是模拟js文件的css导入,但它不起作用。 以下是相关代码: import React from "react"; import ReactDOM from "react-dom"; 从“/样本”中导入样本 导入“/exported.js”; 导入“/styles.css” 我想知道怎样才能做出类似的东西。

我正在使用导入功能,我希望使用类似于css的导入,我的意思是“import./file.css'”,然后所有css属性都扩散到文件中。我用ReactJS尝试过同样的方法,但失败了

我的期望是模拟js文件的css导入,但它不起作用。

以下是相关代码:

import React from "react";
import ReactDOM from "react-dom";
从“/样本”中导入样本 导入“/exported.js”; 导入“/styles.css”

我想知道怎样才能做出类似的东西。任何暗示都很好


谢谢

您需要从“moduleName”导入默认导出以便您可以在代码中使用
defaultExport

执行
导入'moduleName'
将只运行模块中的代码,而不导入任何内容(有关更多信息,请参阅)


在您的沙盒中,执行
从'sample.js'导入样本
就可以了。

沙箱中有问题的代码是:
导入“/exported.js”

令人困惑的一个原因是您正在使用createreact应用程序,它隐藏了webpack的魔力,允许您将CSS文件导入为
import./styles.CSS”。
这不是模块导出和导入的工作方式。我建议阅读有关导出和导入详细信息的部分

您所做的本质上是一个空导入,即您没有导入任何内容,只是执行文件

空导入:只加载模块,不导入任何内容。这个 程序中的此类导入首先执行模块的主体。
导入'src/my_lib'

但简单地说,这里有各种方法可以导入一些东西

假设:您的
/exported.js
文件具有以下导出:

// some other code
export { text1, text2 };
export default config;
然后可以以各种格式导入它们

// import only the default export
import config from './exported.js';
// This only imports the export qualified with default, it ignores others 
// i.e. 
console.log(config); //works
console.log(text1); // fails
console.log(text2); // fails

// import everything the module exports, but as a namespace
import * as myLib from './exported.js';
// usage: all named exports are properties of the myLib object
console.log(myLib.text1); // works
console.log(myLib.text2); // works
console.log(myLib.config); // should not work, unless you have also exported config as a named export

// import only what you need
  import { text1, text2 } from './exported.js';
  console.log(text1); // works
  console.log(text2); // works

// you can also rename them
  import { default as x, text1 as a, text2 as b } from './exported.js';
  console.log(x); // works --> config
  console.log(a); // works --> text1
  console.log(b); // works --> text2

代码中的问题导致中断导入,该导入不允许包含您的css文件,问题是导入export.jssample.js,它必须包括使用正确的解构,例如:

import React from "react";
import ReactDOM from "react-dom";
import { text1, text2 } from "./exported.js";
import sample from "./sample.js";
import "./styles.css";
这里有完整的样品

有关导入语句的更多信息: 解构分配语句:


致以最诚挚的问候。

除了沙盒链接之外,您是否愿意在此处复制/粘贴您的代码?另外,运行应用程序时会发生什么情况?您希望它做什么不同的事情?从“/file.js”导出的
import*是否没有得到您想要的东西?它已经在接收您的CSS了。你还期待什么?请发布代码,而不仅仅是一个链接。是的,对于示例来说,这是我写主题时的一个打字错误,我已经编辑了它,好吧,我看得更清楚了,但在这种情况下,它将在“text1”元素上流动,因此正确地呈现IMHO?它在全局代码上运行,因此“您以不同的方式导出
text1
,因此需要不同的方法来导入它。”。当您命名了导出而不是默认导出时,导入它们的正确语法是从'moduleName'
导入{export}。在您的示例中,您需要从“/exported”执行
import{text1}
@dubes在他的回答中得到了更多的细节。好吧,这意味着我必须创建一个网页魔术,以与css在结尾的方式相同。不,我认为网页魔术太过分了。最好的方法是使用适当的导入语句并遵守规范。这允许您利用任何工具支持改进(例如,单击编辑器中的定义),并且是webpack等工具适当优化捆绑包的重要输入。您对使用答案中提到的导入有什么顾虑吗?“您对使用答案中提到的导入有什么顾虑吗?”这是为了优化我的代码。简单地说,您的精度确实相关
// import only the default export
import config from './exported.js';
// This only imports the export qualified with default, it ignores others 
// i.e. 
console.log(config); //works
console.log(text1); // fails
console.log(text2); // fails

// import everything the module exports, but as a namespace
import * as myLib from './exported.js';
// usage: all named exports are properties of the myLib object
console.log(myLib.text1); // works
console.log(myLib.text2); // works
console.log(myLib.config); // should not work, unless you have also exported config as a named export

// import only what you need
  import { text1, text2 } from './exported.js';
  console.log(text1); // works
  console.log(text2); // works

// you can also rename them
  import { default as x, text1 as a, text2 as b } from './exported.js';
  console.log(x); // works --> config
  console.log(a); // works --> text1
  console.log(b); // works --> text2
import React from "react";
import ReactDOM from "react-dom";
import { text1, text2 } from "./exported.js";
import sample from "./sample.js";
import "./styles.css";