【问题标题】:how to properly manage jquery-ui with webpack如何使用 webpack 正确管理 jquery-ui
【发布时间】:2019-11-23 13:07:16
【问题描述】:

我目前正在我的 symfony 应用程序上迁移到 webpack-encore,我正在努力使用 jquery-ui 库。

在我的第一个文件app.js 我想使用缓动

import $ from 'jquery';
import 'bootstrap';
import 'jquery-ui/ui/effect';


// Smooth scrolling using jQuery easing
$('a.js-scroll-trigger[href*="#"]:not([href="#"])').click(function () {
    if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
        var target = $(this.hash);
        target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
        if (target.length) {
            $('html, body').animate({
                scrollTop: (target.offset().top - 70)
            }, 1000, "easeInOutExpo");
            return false;
        }
    }
});

我花了一些时间才弄清楚它在jqueryui/ui/effect.js 文件中....

现在在我的view.js 我想使用可拖动元素:

//css
import '../css/event.css';

//js 
import $ from 'jquery';
import 'bootstrap';
import 'jquery-ui/ui/plugin';

$("span[class='draggable']").draggable({
    snap : '.dropdown'
});

当然它不起作用我得到一个

错误:$("span[class='draggable']").draggable() 不是函数

有没有合适的方法来加载所有的 jquery-ui 就像整天好?用一个简单的

<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>

【问题讨论】:

    标签: symfony webpack webpack-encore


    【解决方案1】:

    此代码已测试

    安装 jquery-ui 参考:doc

    $ npm install webpack-jquery-ui
    

    然后

    require('webpack-jquery-ui/draggable');
    
    $( "#draggable" ).draggable();
    

    【讨论】:

    • 为什么使用import?因为在doc 中,他们解释说这是官方的ECMAscript 规范。然后我的 javascript 位于我的 HTML 页面的最底部,因此它们是加载的终极目标,我永远不需要 $(document).ready。无论如何,我尝试了您的建议,但它并没有改变任何东西
    • 我已经在我的项目中进行了测试,并用我的工作代码修改了答案,但当我使用“import”时,它仍然给我和你一样的错误。所以我建议你使用“require”
    • 我已经尝试过您的解决方案。它有效地工作,但它与requireimport的使用没有联系,只是你使用了webpack-jquery-ui而不是jquery-ui。原始库与 webpack 原生不兼容?
    • 实际上本地库对我们不利。在 (Doc)[jqueryui.com/upgrade-guide/1.12/#official-package-on-npm] 他们清楚地指定您现在需要单独导入每个小部件。我将暂时使用您的解决方案和webpack-jquery-ui。谢谢
    猜你喜欢
    • 2016-03-04
    • 2017-09-07
    • 2018-06-24
    • 1970-01-01
    • 2016-12-11
    • 2016-04-14
    • 2011-12-05
    • 2021-09-24
    • 1970-01-01
    相关资源
    最近更新 更多