如何合并多个文件中的命名导出并导出为一个对象

时间:2018-03-16 17:26:59

标签: javascript import ecmascript-6 redux

我有很多动作创作者,我试图组织成几个不同的文件。每个单独的文件都导出了以下函数:

module reg4
(
input  [4:0] key,  
output [4:0] led   
);
wire clk;
wire [4:1] d = ~ key [4:1];
global g (.in (~ key [0]), .out (clk));

reg [4:1] q;
always @(posedge clk)
q <= d;
assign led [0] = clk;
assign led [1] = q [1];
assign led [2] = q [2];
assign led [3] = q [3];
assign led [4] = q [4];
endmodule

在将它们分成不同的文件之前,它们的输入方式如下:

export function addData(data) {
  return {
    type: 'ADD_DATA',
    data
  }
}

然而,结构现在像

import { addData } from './actions/actionCreators'

我想将它们合并到索引文件中,并在它们最初命名时导出它们。我试过的是:

动作/ index.js

├─┬ actions
│ ├── actionsCreators1.js
│ ├── actionsCreators2.js
│ └── index.js

但是导入时,命名函数未定义。我可以将它们单独带入import * as actions1 from './actionCreators1' import * as actions2 from './actionCreators2' export default { ...actions1, ...actions2 } 并且它会起作用,但是在两个地方写一切并不理想。

如何合并这些导入并将它们导出为一个对象?

3 个答案:

答案 0 :(得分:3)

您可以使用模块系统本身来传递名称,例如

,而不是依赖于传播
// actions/index.js

export * from './actionCreators1'
export * from './actionCreators2'

因此只要文件中的名称不会发生冲突,它们就会很好地传递。

答案 1 :(得分:1)

在搜索一致的导出/导入模式时,这是一种可能的解决方案。

将其用于所有导入:

def gen_rows(): yield OrderedDict(a=1, b=2) def write_csv(): it = genrows() first_row = it.next() # __next__ in py3 with open("frequencies.csv", "w") as outfile: wr = csv.DictWriter(outfile, fieldnames=list(first_row)) wr.writeheader() wr.writerow(first_row) wr.writerows(it) (没有明星)

对于出口:

import alpha from './alpha'

这会将所有内容分组为export default { ...module.exports, ...alpha } ,以及从export导出的所有内容。

答案 2 :(得分:0)

我认为问题出在您的 actions / index.js

default

由于您要导出为import actions from '../actions' actions.action1(...) ,因此您必须像这样导入:

import { action1 } from '../actions'

actions.action1(...) // Error: action1 is undefined

这将工作:

import * as actions1 from './actionCreators1'
import * as actions2 from './actionCreators2'

export {
  ...actions1, // object spread cannot be used in an export like this
  ...actions2
}

解构语法只能抓取名为的导出,但您使用默认导出。

我希望能够使用对象传播创建命名导出,但是,它是无效的语法:

contract ERC20 {
  function allowance(address owner, address spender) public view returns (uint256);
  function transferFrom(address from, address to, uint256 value) public returns (bool);
  function approve(address spender, uint256 value) public returns (bool);
  function totalSupply() public view returns (uint256);
  function balanceOf(address who) public view returns (uint256);
  function transfer(address to, uint256 value) public returns (bool);

  event Transfer(address indexed from, address indexed to, uint256 value);
  event Approval(address indexed owner, address indexed spender, uint256 value);
}

contract my_contract {
    ERC20 _token;

    function my_contract(address tokenAddress) public {
        _token = ERC20(tokenAddress);
    }

    function my_function(address to, uint amount) public {
        //do_something

        _token.transferFrom(msg.sender, to, amount);
    }
}