不幸的是,我们必须在当前项目中使用需要 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;
}




