Backbone.js 主干和打字脚本有问题
我正在学习主干而不是Typescript,我得到了主干的头文件。我正在尝试创建一个视图,其中我在渲染函数中引用了“this”。但Webstorm在这方面犯了一个错误:Backbone.js 主干和打字脚本有问题,backbone.js,typescript,webstorm,Backbone.js,Typescript,Webstorm,我正在学习主干而不是Typescript,我得到了主干的头文件。我正在尝试创建一个视图,其中我在渲染函数中引用了“this”。但Webstorm在这方面犯了一个错误: class QuestionView extends Backbone.View{ model = new Question(); template: (data:any) => string; constructor(options){ super(options);
class QuestionView extends Backbone.View{
model = new Question();
template: (data:any) => string;
constructor(options){
super(options);
this.tagName = "div";
this.template = _.template($("#add-question").html());
_.bindAll(this, "render");
}
render(){
this.$el.html()(this.template(this.model.toJSON())); // The error is pointing to the first "this" literal in this line
return this;
}
}
错误:
C:/.../Main.ts(40,9): error TS2088: Cannot invoke an expression whose type lacks a call signature.
通常对于
此
问题,它与范围有关,但在您的情况下,它只与html
方法调用中的输入错误有关:
render(){
this.$el.html(this.template(this.model.toJSON())); // fixed
return this;
}
要设置元素的HTML,您需要将HTML传递到HTML
方法的括号内-您不小心没有传递任何参数(因此这会获取HTML,而不是设置HTML),然后尝试像调用函数一样调用生成的HTML字符串
this.$el.html() // returns a string of HTML
this.$el.html('<div>Hello World</div>') // sets the html to the supplied string
this.$el.html()//返回一个html字符串
this.$el.html('Hello World')//将html设置为提供的字符串
其在.d.tsAssuming$el
中的定义是一个jQuery对象,并且jQuery.fn.html
未被覆盖,那么$el.html()
将返回一个字符串。所以this.el.html()(this.template(this.model.toJSON())代码>正在尝试将字符串作为函数调用。也许第一对括号是打字错误?