[英]Why does using floatThead with Angular/TypeScript yield the error Property 'floatThead' does not exist on type 'JQuery<any>'
I need to use the 3rd party jQuery library https://github.com/mkoryak/floatThead .我需要使用第 3 方 jQuery 库https://github.com/mkoryak/floatThead 。
I'm using Bootstrap and jQuery installed from the command line with NPM.我正在使用从命令行安装的 Bootstrap 和 jQuery 以及 NPM。 I've installed floatThead with NPM as well.
我也安装了带有 NPM 的 floatThead。
In my component, I have these imports:在我的组件中,我有这些导入:
import * as $ from 'jquery';
import 'floatThead';
I successfully select the table element like this:我成功 select 表格元素是这样的:
<table class="table table-responsive table-bordered table-hover" #tableElement>
@ViewChild('tableElement')
private tableElement: ElementRef;
And by calling floatThead() like this, I get the above error and see no effect on my site通过像这样调用 floatThead() ,我得到了上述错误,并且在我的网站上看不到任何影响
public ngAfterViewInit(): void {
$(this.tableElement.nativeElement).floatThead({top: 50});
}
Can anyone explain to me why that is?谁能向我解释为什么会这样?
https://stackblitz.com/edit/angular-ivy-xlqrkh https://stackblitz.com/edit/angular-ivy-xlqrkh
Try to use import * as floatThead from 'floatthead';
尝试使用
import * as floatThead from 'floatthead';
And, if it's possible, show us this code on stackblitz并且,如果可能,请在 stackblitz 上向我们展示这段代码
It is because there are no types for floathead
, so even if the script is included, TS does not know that you can call $(xxx).floathead()
是因为
floathead
没有类型,所以即使包含脚本,TS 也不知道可以调用$(xxx).floathead()
What you can do is add jquery
and floathead
to the scripts
section of your angular.json
file and then:您可以做的是将
jquery
和floathead
添加到angular.json
文件的scripts
部分,然后:
Either add the following line below your angular imports在您的 angular 导入下方添加以下行
declare let $: any;
Or, if you've already installed jquery types ( @types/jquery
), you can extend the JQuery interface或者,如果您已经安装了 jquery 类型(
@types/jquery
),您可以扩展 JQuery 接口
interface JQuery
{
floathead(options: any): JQuery;
}
And then, on top of your component file然后,在您的组件文件之上
declare let $: JQuery;
floatThead now comes with typescript types, so update to latest version and it should "just" work. floatThead 现在带有 typescript 类型,所以更新到最新版本,它应该“只是”工作。
https://github.com/mkoryak/floatThead/blob/master/dist/jquery.floatThead.d.ts https://github.com/mkoryak/floatThead/blob/master/dist/jquery.floatThead.d.ts
which in case you were wondering, look like this:如果你想知道,看起来像这样:
interface floatTheadOptions {
position?: string;
scrollContainer?: ($table: JQuery) => JQuery;
responsiveContainer?: ($table: JQuery) => JQuery;
ariaLabel: ($table: JQuery, $headerCell: JQuery, columnIndex: number) => string;
headerCellSelector?: string;
floatTableClass?: string;
floatContainerClass?: string;
top?: number;
bottom?: number;
zIndex?: number;
debug?: boolean;
getSizingRow?: ($table: JQuery, $cols: JQuery, $fthCells: JQuery) => JQuery;
copyTableClass?: boolean;
autoReflow?: boolean;
}
interface JQuery {
floatThead(floatTheadOptions?: floatTheadOptions): JQuery;
floatThead(action: string): JQuery;
trigger(action: string): JQuery;
on(events: string, handler: (event: Event, $floatContainer: JQuery) => void|boolean): JQuery;
on(events: string, handler: (event: Event, isFloated: boolean, $floatContainer: JQuery) => void|boolean): JQuery;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.