首页 > 解决方案 > Jquery 不监听 Typescript 中的事件

问题描述

我正在尝试捕获打开和关闭引导手风琴的事件,但是使用打字稿,事件不会“收听”,但是使用 JS 可以正常工作!

错误:

 module "c:/Users/THIAGOSAAD/Documents/DEVELOPMENT/NEORIS/ALIANSCE/appcomercial/build/typescript/types/jquery"
    This module can only be referenced with ECMAScript imports/exports by turning on the 'allowSyntheticDefaultImports' flag and referencing its default export.ts(2497)

查询类型

export = jquery;
declare function jquery(w: any): any;

班级

import * as $ from '../types/jquery';

export class BootstrapAccordionController {
    private readonly _iconStateContainer: HTMLElement;

    public constructor(private readonly _accordionID: string, public readonly iconStateContainerID: string) {
        this._iconStateContainer = document.getElementById(iconStateContainerID);
    }

    public init(): void {
        try {
            this.changeIconState();
        } catch (error) {
            console.error(error); 
        }
    }

    private changeIconState(): void {
        $(`#${this._accordionID}`).on('shown.bs.collapse',  () => this.createIconState('shown.bs.collapse'));
        $(`#${this._accordionID}`).on('hidden.bs.collapse', () => this.createIconState('hidden.bs.collapse'));
    }

    private createIconState(_stateType: string): void {
        switch(_stateType) {
            case 'shown.bs.collapse':
                this._iconStateContainer.innerHTML = `<h3 data-name="accordion-collapse-icon"><i class="fas fa-angle-up align-middle"></i></h3>`
                break;
            case 'hidden.bs.collapse':
                this._iconStateContainer.innerHTML = `<h3 data-name="accordion-collapse-icon"><i class="fas fa-angle-down align-middle"></i></h3>`
                break;
        }
    }
}

标签: jquerytypescript

解决方案


此模块只能通过打开“allowSyntheticDefaultImports”标志并引用其默认 export.ts(2497) 来引用 ECMAScript 导入/导出

设置esModuleInteroptrue。这反过来也将打开合成默认导入。


推荐阅读