es6-导入所有没有别名的命名模块 [英] es6 - import all named module without alias

查看:114
本文介绍了es6-导入所有没有别名的命名模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们可以导入所有具有别名的命名模块,如下所示,

I know that we can import all named modules with alias as below,

import * as name from "module-name";

参考: https://developer.mozilla. org/zh-CN/docs/Web/JavaScript/Reference/Statements/import

实际上,我已经在A.js中重新导出了我的模块,并且在B.js中继承了同样的模块. PFB.现在,它是两个继承层次,因此导入命名模块并不重要.但是,当我将其继承到5级继承时(A-> B-> C-> D-> E),我需要在所有文件中导入所有命名模块,并需要进行(重新)导出相同的操作在所有.

Actually, I have re-exported my modules in A.js and the same is inherited in B.js. PFB. Now, it's two level of inheritance, so it's not a big deal to import the named modules. But, when I'm taking this to 5 level of inheritance (A -> B -> C -> D -> E), I need to import all named modules in all files and need to do the (re)export the same in all. Instead of doing this,

  • 是否有其他方法可以将所有命名模块的作用域复制到所有级别而无需重复进行操作(导入和导出)
  • 此设计的幕后目的是使它们遵循Opps概念,并避免重新声明相同的模块.

A.js

import React from 'react';
import I18n from 'i18n-js';
import m1 from 'module1';
import m2 from 'module2';

export default class A extends React.Component {}

export {React, I18n, m1, m2)

B.js

import BaseComponent from './A';
import {React, I18n, m1, m2) from './A;

export default class B extends A {}

有什么方法可以导入所有没有别名的命名模块,例如import {*} from './A'(而不是B.js中的第二个)

Is there any way to import all named modules without alias like import {*} from './A' (instead of 2nd in B.js)

推荐答案

有没有办法导入所有没有别名的命名模块,例如从'./A'导入{*}(而不是B.js中的第二个)

Is there any way to import all named modules without alias like import {*} from './A' (instead of 2nd in B.js)

否.

重新导出比您节省行数"更多的整个构想.在最终的js文件中,如您在

And the whole idea of re-exporting more than you need to save the "number of lines" in the final js file as you stated at

因为,它在最终的js文件中为每次导入放置了两行.考虑如果有10条导入行,那么将在最终js中添加20行.当您考虑进行生产时,会太花钱

Because, It's putting two lines for each import in the final js file. Consider If there are 10 import lines than, 20 lines will be added in final js. When you are thinking for production it will too cost

没什么意义,因为这就是JS缩小程序的用途.

Does not make much sense, since that's what JS minifiers are for.

总结:一开始不应该这样做:

To summarise: one should not do that at the very first place:

  1. export仅需要导出的内容
  2. import随处需要.
  3. 您使用JS缩小程序来优化输出JS文件的大小.
  1. You export only what you need to export
  2. You import whatever you need wherever you need.
  3. You use JS minifiers to optimise the output JS file size.

这篇关于es6-导入所有没有别名的命名模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆