不能在 TypeScript 中使用 `$(document).ready`

V0l*_*dek 5 jquery typescript

$(document)在 TypeScript 中使用会出现错误Supplied parameters do not match any signature of call target.

错误画面

我正在使用 TypeScript 3.1、jQuery 3.3.1 和 @types/jQuery 3.3.29。

$(document)弃用,我应该使用别的东西或者是它在类型定义文件的错误?

编辑:这个 TypeScript 文件的整个主体基本上是“Hello World!”。

$(document).ready(() => {
    console.log("Hello World!");
});
Run Code Online (Sandbox Code Playgroud)

Ari*_*ion 12

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

在 jQuery 3.0 中,前两个被弃用,只留下$(handler). 官方理由是:

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

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

$(() => {
    console.log("Hello World!");
});
Run Code Online (Sandbox Code Playgroud)