3

在 Typescript 中订阅 jQuery document.ready() 的一般描述方法似乎如下

class Foo {
    constructor() {
        jQuery(document).ready(() => {
           ...
        });
    }
}

但是在 Typescript 3.0 中我遇到了一个错误..

Supplied parameters do not match any signature of call type...
should have type assignment to string...
but has type 'Document'

这是一个错误还是正确的签名是什么。

4

1 回答 1

2

答案来自Cannot use `$(document).ready` in TypeScript

$(document).ready(handler) 有两个功能等效的变体,第一个是 $().ready(handler),第二个是直接 $(handler)。

在 jQuery 3.0 中,前两个已被弃用,只剩下 $(handler)。官方的理由是:

这是因为选择与 .ready() 方法的行为无关,该方法效率低下并且可能导致对该方法行为的错误假设。

TypeScript 定义文件只是不包含不推荐使用的语法,为了向后兼容,它仍然有效。您的脚本应如下所示:

$(() => {
    console.log("Hello World!");
});
于 2019-04-29T09:15:36.437 回答