【问题标题】:TypeError: Super expression must either be null or a function, not undefined when imported from parent folderTypeError:超级表达式必须为空或函数,从父文件夹导入时未定义
【发布时间】:2018-08-24 01:17:57
【问题描述】:

当我尝试从在索引文件中导出的父文件夹中导入一个类时,我收到此错误:

TypeError: Super expression 必须为 null 或函数,而不是未定义

当我在其他地方导入时,没有问题。

导入:import { BaseComponent } from '../';

父文件夹中的索引文件:

import BaseComponent from './BaseComponent';
import Header from './Header';
import NavigationBar from './NavigationBar';

export { BaseComponent, Header, NavigationBar };

在其他文件中导入:import { BaseComponent, NavigationBar, Header } from './common';

为什么导入在子文件夹中时不起作用?

更新

文件结构:

common/
    BaseComponent/
        index.js
    Header/
        index.js
    NavigationBar/
        index.js (import not working for BaseComponent)
    index.js (export file)
App.js (correct import)

更新 2

此导入确实有效,但我正在尝试使用索引文件来组合类:import BaseComponent from "../BaseComponent";

更新 3

【问题讨论】:

  • 导入路径可能有误。请验证
  • 您使用什么工具来捆绑/解析您的导入?网页包?
  • @ShubhamKhatri 我添加了项目的文件结构
  • @RaphaelSchweikert 这是一个带有 create-react-app 的默认项目
  • 我添加了运行时错误

标签: node.js reactjs npm import


【解决方案1】:

答案以供将来参考

我通过更直接地导出索引文件中的类解决了这个问题。我不知道为什么最初的想法行不通,但是这个解决方案可以解决问题:

export {default as BaseComponent} from './BaseComponent';
export {default as Header} from './Header';
export {default as NavigationBar} from './NavigationBar';

【讨论】:

    【解决方案2】:

    @奥利维尔

    您正在混合使用默认导出语法和命名导出语法。

    你需要匹配导入和导出语法,所以:

    export default BaseComponent { ...
           ^^^^^^^ => export default...
    
    matches
    
    import BaseComponent from '../BaseComponent'
           ^^^^^^^^^^^^^  ==> import witout {}
    

    export BaseComponent { ...
           ^^^^^^^^^^^^^  => named export
    matches
    
    import { BaseComponent } from '../BaseComponent'
          ^^              ^^ ==> use {} to named imports! 
    

    【讨论】:

    • 不完全是我的意思,我知道导入和导出的方式。它更多的是关于导入项目并在同一个文件中再次导出它们。
    猜你喜欢
    • 2017-07-11
    • 2023-04-10
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2018-01-21
    • 2018-12-21
    • 1970-01-01
    相关资源
    最近更新 更多