typescript - TypeScript:如何调用父类中的函数?
问题描述
在 nodes.tsx 我正在导入 InputHandler
import { InputHandler } from './inputHandler';
class Nodes {
canvasWidth: number;
canvasHeight: number;
constructor(width: number, height: number) {
this.canvasWidth = width;
this.canvasHeight = height;
new InputHandler(this);
// ^ error
}
clicked() {
console.log('clicked');
}
}
export { Nodes };
我得到错误unused expression, expected an assignment or function call (no-unused-expression)tslint(1)
class InputHandler {
constructor(nodes: any) {
// ^ error, im passing in `this` from the parent
document.addEventListener('click', (event) => {
nodes.clicked();
});
}
}
export { InputHandler };
在输入处理程序内部,我正在尝试调用clicked()
父级。我也收到错误
Type declaration of 'any' loses type-safety. Consider replacing it with a more precise type. (no-any)tslint(1)
当我单击画布或页面时,不会调用 clicked() 函数。
我觉得整个模式是错误的?
解决方案
这不是你使用构造函数的方式:
const handler = new InputHandler(this);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor
推荐阅读
- javascript - 在 npm 包中导出构造函数和枚举
- c++ - 为什么是 `r(sqrt(x*x + y*y))` 而不是 `r(x)`
- java - 选择使用哪个片段的 Android (Java) Activity
- php - Login.php 不重定向
- report - IIF 表达式 - 重载解析错误
- mongodb - MongoDB - 仅显示选定信息的数据
- javascript - 在加入会话 openvidu-server 的第二次请求时获取 409 api/session
- c - 有人有非异步安全信号处理程序死锁的示例吗
- asp.net-core - ASP.NET Entity Framework 在搭建脚手架时从视图中隐藏模型属性
- google-apps-script - Adding Replace Existing File to GmailToDrive App Script