简体   繁体   English

简化需求/导入

[英]Simplifying requires/imports

I'm learning ReactJS right now and trying to separate all the components into discrete files. 我现在正在学习ReactJS,并尝试将所有组件分离为离散文件。

Is there some strategy to simplify the process of requiring/importing components? 是否有一些策略可以简化要求/导入组件的过程? It seems rather silly to have import React from 'react'; import React from 'react';似乎很愚蠢import React from 'react'; on the first line of every component file? 在每个组件文件的第一行?

I'm using Babel for the import function. 我正在使用Babel import功能。

I believe that's the recommended way if you want to use the import/export mechanism from ES6 to separate the component. 如果您想使用ES6的导入/导出机制来分离组件,我相信这是推荐的方法。 A way to not do this is to assign React as a global variable, but that kinda defeat the purpose of using import/export. 一种不执行此操作的方法是将React分配为全局变量,但这有点违反了使用导入/导出的目的。

Indeed we need to do similar things in other languages like Java? 确实,我们需要在其他语言(如Java)中做类似的事情吗?

To get around writing import React from 'react' every time, you can use Webpack and: 为了避免每次都import React from 'react'编写import React from 'react' ,可以使用Webpack和:

  1. write a loader that adds the statement import React from 'react' to every JSX file it encounters; 编写一个加载器,将遇到的每个JSX文件中的import React from 'react'语句添加到它遇到的每个JSX文件中; or, 要么,
  2. use React from a CDN and include it as a Webpack external in your configuration. 从CDN使用React,并将其作为Webpack外部包含在您的配置中。

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

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