简体   繁体   English

使用带有es6导入的jquery插件

[英]Using a jquery plugin with es6 import

I am trying to import rangeslider into an es6 class using import, however it always returns rangeslider is not a function 我试图使用import将rangelider导入到es6类中,但是它总是返回rangeslider is not a function

import * as rangeslider from 'rangeslider.js';

export class Layout {

    testFunc() {
        $(".rangepicker").rangeslider({
            onSlide: function(position, value)
            {
                $(this).parent().find(".rangepicker-output").html(value + 'px');
            }
        });
    }
}

The below is rangeslider.js 's source code, that do not offer es6 modular support. 以下是rangeslider.js的源代码,它不提供es6模块支持。 It support AMD and CommonJs,but not es6. 它支持AMD和CommonJs,但不支持es6。

function(factory) {
'use strict';

if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['jquery'], factory);
} else if (typeof exports === 'object') {
    // CommonJS
    module.exports = factory(require('jquery'));
} else {
    // Browser globals
    factory(jQuery);
}

The es6 modular is like: es6模块化如下:

export default function foo() {
  console.log('foo');
}

It doesn't make sense to import exported value from jQuery plugins because they conventionally export jQuery instance. 从jQuery插件导入导出值没有意义,因为它们通常导出jQuery实例。

The fact that imported rangeslider isn't used in code eliminates it from transpiled code, the package is not imported at all. 代码中未使用导入的rangeslider这一事实将其从转换后的代码中删除,根本不会导入包。

It should be 它应该是

import 'rangeslider.js';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM