简体   繁体   English

ES6导入导入未定义

[英]ES6 import importing undefined

I have two files in my react app: 我的反应应用程序中有两个文件:

/* MyApp/components/my-component.jsx */

export class MyComponent extends React.Component {
  // ...
};

console.log(MyComponent); // (1)

And

/* MyApp/my-app.jsx */

import MyComponent from './components/my-component';

console.log(MyComponent); // (2)

console.log number (1) gives me this: function MyComponent(props, context) {... . console.log number (1)给我这个: function MyComponent(props, context) {... But console.log number (2) gives me undefined . 但是console.log number (2)给了我undefined

What am I doing wrong? 我究竟做错了什么? It seems pretty straightforward and yet won't work. 这似乎很简单,但不起作用。

Look in the documentation: 查看文档:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

The following form of the import statement is only for a module with a default export. 以下形式的import语句仅适用于具有默认导出的模块。

import MyComponent from './components/my-component';

You need to do this: 你需要这样做:

import {MyComponent} from './components/my-component';

Or export your class as the default, then the import will work as you wrote it: 或者将您的类导出为默认值,然后导入将在您编写时起作用:

export default class MyComponent extends React.Component {
  // ...
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM