【问题标题】:Converting "this" from jquery to typescript将“this”从 jquery 转换为 typescript
【发布时间】:2016-11-05 00:53:08
【问题描述】:

我有这段代码可以工作。

我现在想在打字稿应用程序中使用它。我还是想用jquery,但是不明白怎么把this的用法转换成Typescript需要工作的东西。

if ($(this).width() < 769) {
    $('body').addClass('page-small');
} else {
    $('body').removeClass('page-small');
    $('body').removeClass('show-sidebar');
}

在 typescript 中,this 指的是当前类。我应该用什么替换它才能按预期工作?

【问题讨论】:

  • 您混淆了this 上下文。假设显示的代码将在事件处理程序中 this 将具有该回调函数的上下文
  • Substitute Javascript this to Typescript 的可能重复项——这将回答您的问题。

标签: jquery typescript typescript1.8


【解决方案1】:

我仍然想使用 jquery,但我不明白如何将 this 的使用转换为 Typescript 需要的工作。

将函数移出类并从类中调用它。或者,创建一个self 变量以同时使用this(class) 和this(jquery)。

更多

https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html#tip-arrow-functions-with-libraries-that-use-this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多