[英]Having problem with import in ES6 module: property is not exported
I'm currently working with redux, and I'm exporting an object with the type properties to my reducer file.我目前正在使用 redux,并且正在将具有类型属性的对象导出到我的 reducer 文件中。
My types.js file:我的 types.js 文件:
const Types = {LOG_IN:'LOG_IN',REGISTER:'REGISTER'};
export default Types;
My reducer.js file:我的 reducer.js 文件:
import {LOG_IN,REGISTER} from './types';
//CODE HERE
When I compile, however, CRA give me an error:但是,当我编译时,CRA 给我一个错误:
Attempted import error: 'LOG_IN' is not exported from './types'.
Curiously enough, this code works:奇怪的是,这段代码有效:
import Types from './types';
const {LOG_IN,REGISTER} = Types;
Why is it that my code structure doesn't work with that form of importing while React's and Redux's code does?为什么我的代码结构不适用于那种导入形式,而 React 和 Redux 的代码却可以? Thank you in advance.
先感谢您。
You need to explicitly define a named export for each constant for that to work.您需要为每个常量显式定义一个命名导出以使其工作。
export const LOG_IN = 'LOG_IN';
export const REGISTER = 'REGISTER';
Importing a named export is not the same thing as destructuring a default export, which is what's happening when you do this:导入命名导出与解构默认导出不同,这是您执行此操作时发生的情况:
import Types from './types';
const {LOG_IN,REGISTER} = Types;
You can read more on this here .您可以在此处阅读更多相关信息。 Other code you find in the wild might appear to work like you describe because it's actually defining its exports like this:
您在野外找到的其他代码可能看起来像您描述的那样工作,因为它实际上是这样定义其导出的:
module.exports = {
LOG_IN: 'LOG_IN'
}
I think is because you export Types , if tou can use LOG_IN for example you need to access Types.LOG_IN , and the code const {LOG_IN,REGISTER} = Types;我认为是因为你导出Types ,如果你可以使用LOG_IN例如你需要访问Types.LOG_IN ,并且代码const {LOG_IN,REGISTER} = Types; works because you are using destructuring
有效,因为您正在使用解构
the这
import Types from './types';
are correct是正确的
an then try to access with Types.REGISTER
然后尝试使用
Types.REGISTER
访问
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.