重新导出模块不适用于对象传播 [英] Re-exporting modules does not work with object spread

查看:106
本文介绍了重新导出模块不适用于对象传播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 index.js 文件,内容如下:

i have an index.js file that reads:

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是我期望的函数,选择器别名是具有我期望的选择器的键的对象。 Reducer是duck文件的默认导出,而选择器是来自相同文件的导出。

If I console.log in this file I see that the reducers are functions I expect and the selectors alias are objects with keys to the selectors I expect. The reducers are default exports for the duck files and the selectors are exports from the same respective file.

但是,当我尝试用另一个文件导入此模块时,我只是能够导入两个减速器。两个选择器未定义。我认为解构会将每个键添加到我的导出对象。我究竟做错了什么?

However, when I try to import this module with another file I am only able to import the two reducers. The two selectors are undefined. I thought that de-structuring would add each key to my export object. What am I doing wrong?

other_file1.js

import { aReducer, bReducer } from 'my-module'; //works!

other_file2.js

import { someSelectorThatWasInMyaSelectorsObject } from 'my-module'; //does NOT work!


推荐答案

您不能使用 .. 。 export {}; 块中。它是一个明确的名称列表,就像中导入{name}一样。它不是导出键的对象。例如与导入相同的方式

You cannot use ... in an export {}; block. It is an explicit list of names just like import {name} from is. It is not an object with keys being exported. e.g. the same way imports do

import { foo as fooRenamed } from "";

export 它是

export {
  fooVar as foo,
};

export 块是一个明确的列表要导出的变量,以及导出的可选显式名称。没有涉及的对象。

The export block is an explicit list of variables to export, with an optional explicit name for the export. There are no objects involved.

具体来说,没有涉及对象,因为在文件正文执行之前处理和知道导出的名称,所以不会只有对象不被允许,它们是不可能允许的,因为对象需要执行才能存在。

Specifically, there are no objects involved because the names of the exports are processed and known before the body of the file has even executed, so not only are objects not allowed, they are impossible to allow because objects require execution to exist.

要获得你想要的东西,你应该使用:

To get what you'd want, you should use:

// 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';

这篇关于重新导出模块不适用于对象传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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