首页 > 解决方案 > 在 React 中调用类的 Webpack “不是函数”错误

问题描述

在一个反应​​应用程序中,我有一些业务逻辑试图封装在名为 Authentication 的类中。当我尝试在 Authentication 类上调用名为 configure 的方法时,出现以下错误:

类型错误: WEBPACK_IMPORTED_MODULE_5__authentication_js .a.configure 不是函数

在我的 React app.js 文件中,我使用以下命令导入身份验证类:

import Authentication from './authentication.js';

然后我尝试在我的构造函数中访问它,如下所示:

constructor(props) {
    super(props);
    Authentication.configure();
}

authentication.js的代码如下:

class Authentication {

  constructor(props) {
    console.log('inside constructor of Authentication class');    
  }

  configure(){
    //some setup logic, still fails if I comment it all out
  }
}
export default Authentication;

我不确定如何准确解决此问题。我知道我之前在 react 中看到过类似的问题,内部方法没有针对它调用 bind 方法,但我不确定外部类的公共方法是否需要它,如果需要,我我不知道如何实现它。任何帮助/指导将不胜感激。

标签: reactjses6-class

解决方案


configureAuthentication类的实例方法。

要么制作configure static要么导出Authentication.


推荐阅读