gpt4 book ai didi

jquery - Angular2 和 typescript : Error TS2339: Property 'selectpicker' does not exist on type 'ElementFinder'

转载 作者:行者123 更新时间:2023-12-02 11:01:25 25 4
gpt4 key购买 nike

不幸的是,我们必须在当前项目中使用需要 jQuery 的旧库。这些的激活发生在生命周期钩子(Hook)部分“ngAfterViewChecked”中。由于事实上,TypeScript 无法在正确的时间解决它,它输出错误:

Error TS2339: Property 'selectpicker' does not exist on type 'ElementFinder'.



那是相关的代码块:
 ngAfterViewChecked () {
if (this.agency) {
//noinspection TypeScriptUnresolvedFunction
$('.selectpicker').selectpicker();
}
}

我添加了 //noinspection TypeScriptUnresolvedFunction 行在 selectpicker 激活之前,但它没有帮助。

我们有一个预配置的构建系统,由于这些错误,它“反弹”了我们。我怎样才能摆脱这个问题?

最佳答案

您必须扩展 jQuery 的类型定义。这意味着将 jQuery 命名空间与您的自定义 .d.ts 合并定义了.selectpicker()方法。请参阅文档以获取帮助:

https://www.typescriptlang.org/docs/handbook/writing-declaration-files.html

根据您的 jQuery 定义文件,这可能有效:

interface JQuery {
selectpicker: () => void;
}

关于jquery - Angular2 和 typescript : Error TS2339: Property 'selectpicker' does not exist on type 'ElementFinder' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39075161/

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