reactjs - 在 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 方法,但我不确定外部类的公共方法是否需要它,如果需要,我我不知道如何实现它。任何帮助/指导将不胜感激。
解决方案
configure
是Authentication
类的实例方法。
要么制作configure
static,要么导出Authentication
.
推荐阅读
- azure-cosmosdb - 使用 Cosmos 客户端返回 Azure Cosmos 项的平面层次结构
- python - TensorFlow 操作的 shared_name
- javascript - 表单 AJAX PHP 不会将 POST 数据发送到 WordPress 数据库
- vb.net - Windows 窗体:访问控件外观属性
- javascript - 在 Express 应用程序中发出请求的两种方式之间的区别
- julia - 在远程进程中更改/访问远程变量
- css - two border color example explained
- express - mongoose query return array instead of object
- python - Python:如果名称用逗号分隔并在单独的行上,如何从文件中读取名称?
- c++ - 如何在 SFML for C++ 中单独访问每个形状/对象