gpt4 book ai didi

typescript3.0 - 带有 $(document).ready 的 typescript 3.0

转载 作者:行者123 更新时间:2023-12-02 00:58:09 26 4
gpt4 key购买 nike

在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'

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

最佳答案

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

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

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

This is because selection has no bearing on the behavior of the .ready() method, which is inefficient and can lead to incorrect assumptions about the method's behavior.

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

$(() => {
console.log("Hello World!");
});

关于typescript3.0 - 带有 $(document).ready 的 typescript 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52648378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com