如何使JQuery / JQueryUI与React兼容?

时间:2019-01-14 12:56:22

标签: javascript jquery reactjs jquery-ui

我正在将一些HTML代码转换为ReactJS,并且在使用Jquery / JqueryUI时遇到问题。

我使用以下命令导入两个文件:

import * as jQuery from 'jquery';
import * as jui from 'jquery-ui';

在我的componentDidMount()中,

componentDidMount() {
    jQuery(function () { 
        jQuery("#palettes").accordion({
            activate: function (event, ui) {
                furniturePalette.requestUpdate();
                //  wallPartsPalette.requestUpdate();
            }
        });
        jQuery("#openDocument").draggable({ handle: "#openDocumentHandle", stack: ".draggable", containment: 'window', scroll: false });
        jQuery('#optionsWindow').draggable({ handle: "#optionsWindowHandle", stack: ".draggable", containment: 'window', scroll: false });
        jQuery("#removeDocument").draggable({ handle: "#removeDocumentHandle", stack: ".draggable", containment: 'window', scroll: false });
        jQuery("#myOverviewWindow").draggable({ handle: "#myOverviewWindowHandle", stack: ".draggable", containment: 'window', scroll: false });
        jQuery('#statisticsWindow').draggable({ handle: "#statisticsWindowHandle", stack: ".draggable", containment: 'window', scroll: false });
        jQuery("#selectionInfoWindow").draggable({ handle: "#selectionInfoWindowHandle", stack: ".draggable", containment: 'window', scroll: false });
        jQuery("#myPaletteWindow").draggable({ handle: "#myPaletteWindowHandle", stack: ".draggable", containment: 'window', scroll: false });
        jQuery("#myPaletteWindow").resize(function () {
            furniturePalette.requestUpdate();
            //wallPartsPalette.requestUpdate();
        });
    });

我使用'jQuery'而不是'$,因为'$'与我正在使用的另一个库一起使用。

运行此命令我会得到

TypeError: __WEBPACK_IMPORTED_MODULE_8_jquery__(...).accordion is not a function

如果将“ jQuery”替换为“ jui”,我将得到:

TypeError: name.split is not a function

(此错误在jquery-ui子目录中)。

有人可以解决吗?

解决方案

只需写:

import * as  jQuery from 'jquery';
import 'jquery-ui-bundle';

1 个答案:

答案 0 :(得分:0)

import * as  jQuery from 'jquery';
import 'jquery-ui-bundle';