javascript - nodejs 超级表达式必须为 null 或函数
问题描述
在我的 UserService 类中,我试图用 super 调用我的基类构造函数。如果我尝试这样做,我会收到以下错误:TypeError: Super expression must either be null or a function
这是我的 UserService 类
import { Service } from './Service';
class UserService extends Service {
constructor(model) {
//Calls Service contructor with specified model.
super(model);
}
}
export default { UserService };
这是基类:
import autoBind from 'auto-bind';
class Service {
constructor(model) {
this.model = model;
autoBind(this);
}
}
export default { Service };
看了一圈,有人说可能跟类名拼写不正确有关。我检查了我的,但它们都是正确的。
其他人说这可能是这些类的导出和导入方式。
我对不同的导出语法不是很熟悉,所以这可能是问题所在?
更新:
这确实与我进出口事物的方式有关。
解决方案
无法重现您的问题。您可能没有正确导入服务。这是一个工作示例:
Service.js
:
import autoBind from 'auto-bind';
class Service {
model;
constructor(model) {
this.model = model;
autoBind(this);
}
}
export { Service };
UserService.js
:
import { Service } from './Service';
class UserService extends Service {
constructor(model) {
super(model);
}
}
export { UserService };
main.js
:
import { UserService } from './UserService';
const model = { name: 'teresa teng' };
const userService = new UserService(model);
console.log(userService.model.name);
输出:
teresa teng
推荐阅读
- rust - 在 Rust 中,为什么 `void*` 是 `*mut c_void` 而不是 `*mut ()`?我可以使用 `*mut ()` 安全地表示 `void*` 地址吗?
- java - 如何从 txt 文件中打印特定数字?
- php - 为什么我的 $_SESSION 超级全局变量不能跨多个页面持久化?
- c++ - 有没有比使用蛮力更快的方法来对向量执行加权平均?
- .net - Error Swagger Ambiguous HTTP 操作方法
- statistics - 试图排除占星术但出了点问题
- java - 有没有办法拒绝 jar 启动但只允许启动器(.exe)启动它?
- windows - 无法删除部分子项。这可能是因为进程打开了文件或将其工作目录设置在目标目录中
- javascript - 在 React 中迭代网格和卡片中的值
- java - 如何检测应用程序是否在 NOX/MEMUPLAY 模拟器上运行?