麻烦骨干网和打字稿 [英] Trouble with Backbone and Typescript

查看:261
本文介绍了麻烦骨干网和打字稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习了打字稿骨干,我得到了骨干头文件。我想创造一个我引用本的渲染功能,内部的视图。但Webstorm是上抛出一个错误:

 类QuestionView扩展Backbone.View {
    模型=新问题();
    模板:(数据:任何)=>串;    构造器(选件){
        超(选件);
        this.tagName =格;
        this.template = _.template($(#附加问题)HTML());
        _.bindAll(这一点,渲染);
    }    渲染(){
        。这$ el.html()(this.template(this.model.toJSON())); //错误指向先在该行本字面
        返回此;
    }
}

错误:

  C:/.../ Main.ts(40,9):错误TS2088:不能援引一名前pression其类型缺少调用签名。


解决方案

通常与这个的问题是涉及到的范围,但在你的情况下,它只是涉及到错字在你的 HTML 方法调用:

 渲染(){
    这$ el.html(this.template(this.model.toJSON())); //固定
    返回此;
}

要设置元素的HTML,你通过 HTML 方法的括号内的HTML - 你不小心没有通过参数(所以这得到HTML,而不是设置HTML),然后试图调用生成的HTML的字符串像一个函数。

 这一点。$ el.html()//返回HTML字符串
这$ el.html('< D​​IV>的Hello World< / DIV>')//设置HTML所提供的字符串

I am learning backbone over Typescript and I got the header file for backbone. I am trying to create a view in which I've referenced "this" inside the render function. But Webstorm is throwing an error on that:

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;
    }
}

Error:

C:/.../Main.ts(40,9): error TS2088: Cannot invoke an expression whose type lacks a call signature.

解决方案

Usually with this problems it is related to scope, but in your case it is just related to a typo in your html method call:

render(){
    this.$el.html(this.template(this.model.toJSON())); // fixed
    return this;
}

To set the HTML of an element, you pass the HTML inside the parenthesis of the html method - you accidentally passed no arguments (so this GETS the HTML, rather than SETS the HTML) and then tried to call the resulting string of HTML like a function.

this.$el.html() // returns a string of HTML
this.$el.html('<div>Hello World</div>') // sets the html to the supplied string

这篇关于麻烦骨干网和打字稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆