简体   繁体   English

为什么使用带有 Angular/TypeScript 的 floatThead 会产生错误 Property 'floatThead' does not exist on type 'JQuery<any> '</any>

[英]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:您可以做的是将jqueryfloathead添加到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.

相关问题 使用jQuery floatthead修复的HTML标头。 - HTML Header fixed using jquery floatthead. 带有打字稿的“JQuery”类型上不存在属性“滑块” - Property 'slider' does not exist on type 'JQuery' with typescript 在IE10中使用jQuery floatThead插件(1.2.10)的布局问题 - Layout issue using jQuery floatThead plugin(1.2.10) in IE10 Angular 2属性“ jQuery”在Window类型上不存在 - Angular 2 Property 'jQuery' does not exist on type Window 错误:“类型 jQuery 上不存在属性上下文” - Error: 'Property context does not exist on type jQuery' 使用jQuery floatThead插件调整jQuery UI大小 - jQuery UI resizable with jQuery floatThead plugin Typescript 中的 jQuery:类型“JQueryStatic”上不存在属性“attr”<HTMLElement> &#39;.ts - jQuery in Typescript: Property 'attr' does not exist on type 'JQueryStatic<HTMLElement>'.ts Angular2和Typescript:错误TS2339:类型“ ElementFinder”上不存在属性“ selectpicker” - Angular2 & Typescript: Error TS2339: Property 'selectpicker' does not exist on type 'ElementFinder' 打字稿错误:类型“元素”上不存在属性“包含”。 - Typescript error :Property 'contains' does not exist on type 'Element'.? 使用 Typescript 时如何停止“JQuery 类型上不存在属性”语法错误? - How can I stop “property does not exist on type JQuery” syntax errors when using Typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM