Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 导入ES6样式,如*但用于常量集以避免重复_Javascript_Webpack_Ecmascript 6_Babeljs - Fatal编程技术网

Javascript 导入ES6样式,如*但用于常量集以避免重复

Javascript 导入ES6样式,如*但用于常量集以避免重复,javascript,webpack,ecmascript-6,babeljs,Javascript,Webpack,Ecmascript 6,Babeljs,目前,我有如下代码: import { actionA, actionB, actionC } from './someModule' doStuff({actionA, actionB, actionC}) 您可以看到,列出操作时存在重复,这是我关心的问题,我希望在列出操作时避免重复。所以我想要这样的东西: import { actionA, actionB, actionC } as actions from './someModule' doStuff(actions) //someM

目前,我有如下代码:

import { actionA, actionB, actionC } from './someModule'
doStuff({actionA, actionB, actionC})
您可以看到,列出操作时存在重复,这是我关心的问题,我希望在列出操作时避免重复。所以我想要这样的东西:

import { actionA, actionB, actionC } as actions from './someModule'
doStuff(actions)
//someModule.js看起来像:

export const actionA = ...
export const actionB = ...
export const actionC = ...
export const actionD = ...
... and so on

我不需要全部,因为可能还有更多与当前工作无关的内容需要导入

更新

我在这篇文章中解释了同样的话题。下同

根据,您可以将整个模块内容(如
*作为常量)上的别名设置为常量,也可以将单个内容(如
b)设置为常量。但不能对特定内容设置别名。因此,解决方案之一就是使用*

import * as constants from './module1';
另一种可能的解决方案是将
{a,c}
作为默认值传递

//module1.js

export const a = ...
export const b = ...
export const c = ...
export const d = ...
export default { a, b, c };

/module2.js
import contants from './someModule';
doStuff(constatns);
最后,如果不想将这些常量作为默认值传递,可以创建一个对象并传递该对象

//module1.js

export const a = ...
export const b = ...
export const c = ...
export const b = ...
export const myCustomConstants = { a, c };

//module2.js
import { myCustomConstants } from './someModule';
doStuff(myCustomConstants);

更新

我在这篇文章中解释了同样的话题。下同

根据,您可以将整个模块内容(如
*作为常量)上的别名设置为常量,也可以将单个内容(如
b)设置为常量。但不能对特定内容设置别名。因此,解决方案之一就是使用*

import * as constants from './module1';
另一种可能的解决方案是将
{a,c}
作为默认值传递

//module1.js

export const a = ...
export const b = ...
export const c = ...
export const d = ...
export default { a, b, c };

/module2.js
import contants from './someModule';
doStuff(constatns);
最后,如果不想将这些常量作为默认值传递,可以创建一个对象并传递该对象

//module1.js

export const a = ...
export const b = ...
export const c = ...
export const b = ...
export const myCustomConstants = { a, c };

//module2.js
import { myCustomConstants } from './someModule';
doStuff(myCustomConstants);

如何导出模块?例如:export const actionA=。。。(更新的描述)是的,一个重复的-closing如何导出模块?比如:export const actionA=。。。(更新描述)是的,重复-关闭谢谢,这是一个很好的方法。虽然我主要关心的是避免重复。所谓重复,我的意思是列出行动的名称。因此,当我在main.js中列出3个动作名称,同时在someModule.js中列出它们时,我理解这是一个重复。在这种情况下,可以导出所有动作,并使用lodash pick方法拾取所需的动作。如果有帮助的话,我会更新我的答案。你可能想删除这个答案,而不是把精力放在重复的目标问题上。谢谢,这是一个很好的方法。虽然我主要关心的是避免重复。所谓重复,我的意思是列出行动的名称。因此,当我在main.js中列出3个动作名称,同时在someModule.js中列出它们时,我理解这是一个重复。在这种情况下,可以导出所有动作,并使用lodash pick方法拾取所需的动作。如果有帮助的话,我会更新我的答案。你可能想删除这个答案,而不是把所有的努力放在重复的目标问题上。