【发布时间】:2017-03-01 08:34:02
【问题描述】:
我写了以下代码:
export class ClickHandlers {
static entityNameClickHandler(id) {
console.log("EntityNameClickHandler");
// add the new subTree to the end of historySubTree
this.history.addToHistory(id);
this.refreshEntityContentElem(this);
}
}
在另一个文件中:
addEventListenersToEntityInExplorer(elem, id) {
elem.find('[data-id ^= "expand_"]').click(ClickHandlers.expandButtonHandler);
elem.find('[data-id ^= "entity"]').click(function() {
ClickHandlers.entityNameClickHandler.call(this, id)
}.bind(this));
}
我收到以下行错误: this.history.addToHistory(id); this.refreshEntityContentElem(this);
错误是:
错误:(25, 12) TS2339: 类型上不存在属性“历史” 'typeof ClickHandlers'。
据我所知,Typescript 将“this”视为 ClickHanlders 类。但是,我使用'call'调用了函数'ClickHandlers.entityNameClickHandler',所以'this'不一定是包装对象。
【问题讨论】:
-
这是一个静态方法,然后没有任何实例
-
好的。那么为什么我给它的“这个”有问题呢?
-
没有。它不是。这是打字稿问题。
-
Typescript 不绑定静态方法'
this。它编译为正常ClassName.method = function (args) { ...
标签: javascript typescript this