TypeScript:访问类的静态方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18662095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript: access static method of a class
提问by DT DT
Still new at TypeScript so this question may sound dumb to some of you. I have a ToolTip class like this:
在 TypeScript 中仍然是新手,所以这个问题对你们中的一些人来说可能听起来很愚蠢。我有一个像这样的 ToolTip 类:
class ToolTip{
public static show (str:string):void{
console.log ("ToolTip show():" + str);
}
public static hide():void{
console.log ("ToolTip hide()");
}
}
export = ToolTip;
And I want to call it from another class
我想从另一个班级调用它
import ToolTip = require ("app/view/common/Tooltip");
class Button {
......
private handleMouseEvent(event:MouseEvent):void {
switch (event.type) {
case "mouseover":
ToolTip.show("tool tip string");
break;
case "mouseout":
ToolTip.hide();
break;
}
}
......
}
export = MenuItem;
But it gives me this error:
但它给了我这个错误:
Uncaught TypeError: Object app/view/common/Tooltip has no method 'show'
Any idea how to fix this?
知道如何解决这个问题吗?
采纳答案by basarat
As you can see the code works fine (compiles andruns) :
如您所见,代码运行良好(编译并运行):
So possible reasons why it would not work for you :
所以它对你不起作用的可能原因:
- You did not compile with the
--module commonjs
option (Video Tutorial) - You have a folder named TootTip at the same level as
Tooltip.ts
which can cause nodejs to run what you might not have expected.
- 您没有使用该
--module commonjs
选项进行编译(视频教程) - 您在同一级别有一个名为 TootTip 的文件夹,
Tooltip.ts
它可能会导致 nodejs 运行您可能没有预料到的内容。