将Typescript函数作为Javascript函数传递 [英] Pass Typescript function as a Javascript function

查看:85
本文介绍了将Typescript函数作为Javascript函数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我轻而易举地使用打字稿.如何将Typescript函数传递给 executeQuery.then ?

I use typescript with breeze. How can i pass a typescript function to executeQuery.then?

class MyClass{
 ...
    myFunc(data:any):void{
       ...
    }

    doQuery():void{
        var manager = new breeze.EntityManager('/breeze/dbentities');
        var query = breeze.EntityQuery.from("Corporations").where("Name", "startsWith", "Zen");
        manager.executeQuery(query)
               .then(this.myFunc);  // does not work!
    }
}

推荐答案

使用 this.myFunc 代替 myFunc .

Use this.myFunc instead of myFunc.

这可能是上下文问题.尝试使用 this.myFunc.bind(this)而不是 this.myFunc .

It might be a context problem. Try this.myFunc.bind(this) instead of this.myFunc.

有关上下文的更多信息,请参考文章来自MDN.

For more information about context, refer "this" and "Function.prototype.bind" article from MDN.

这篇关于将Typescript函数作为Javascript函数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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