我有一个index.js
文件,内容如下:
import aReducer from './ducks/a';
import bReducer from './ducks/b';
import * as aSelectors from './ducks/a';
import * as bSelectors from './ducks/b';
console.log(aReducer) //function i expect
console.log(aSelectors) //object with keys to selectors i expect
export { aReducer, bReducer, ...aSelectors, ...bSelectors };
如果我在这个文件中console.log
,我看到reducers是我期望的函数,而selectors别名是具有我期望的选择器键的对象。 reducer是duck文件的默认导出,选择器是从相同的文件导出。
但是,当我尝试使用其他文件导入此模块时,我只能导入两个reducer。两个选择器未定义。我认为解构会将每个键添加到我的导出对象。我究竟做错了什么?
other_file1.js
import { aReducer, bReducer } from 'my-module'; //works!
other_file2.js
import { someSelectorThatWasInMyaSelectorsObject } from 'my-module'; //does NOT work!
答案 0 :(得分:2)
您无法在...
块中使用export {};
。它是一个明确的名称列表,就像import {name} from
一样。它不是导出键的对象。例如与进口相同的方式
import { foo as fooRenamed } from "";
export
export {
fooVar as foo,
};
export
块是要导出的变量的显式列表,其中包含导出的可选显式名称。没有涉及的对象。
具体来说,没有涉及对象,因为在文件正文甚至执行之前处理和知道导出的名称,因此不仅不允许对象,因为对象需要执行才能存在。
要获得您想要的东西,您应该使用:
// Export the referenced files' default under two specific names.
export { default as aReducer } from './ducks/a';
export { default as bReducer } from './ducks/b';
// Re-export every named export from these two files.
export * from './ducks/a';
export * from './ducks/b';