Tur*_*ets 6 javascript import export ecmascript-6 es6-modules
我有一个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 };
Run Code Online (Sandbox Code Playgroud)
如果我console.log
在这个文件中,我会看到化简器是我期望的函数,而选择器别名是带有我期望的选择器键的对象。减速器是鸭子文件的默认导出,选择器是来自同一相应文件的导出。
但是,当我尝试使用另一个文件导入此模块时,我只能导入两个减速器。这两个选择器未定义。我认为解构会将每个键添加到我的导出对象中。我究竟做错了什么?
other_file1.js
import { aReducer, bReducer } from 'my-module'; //works!
Run Code Online (Sandbox Code Playgroud)
other_file2.js
import { someSelectorThatWasInMyaSelectorsObject } from 'my-module'; //does NOT work!
Run Code Online (Sandbox Code Playgroud)
不能...
在export {};
块中使用。它是一个明确的名称列表,就像import {name} from
is 一样。它不是一个带有导出密钥的对象。例如,与导入相同
import { foo as fooRenamed } from "";
Run Code Online (Sandbox Code Playgroud)
与export
它是
export {
fooVar as foo,
};
Run Code Online (Sandbox Code Playgroud)
该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';
Run Code Online (Sandbox Code Playgroud)