$(…).datetimepicker is not a function

末鹿安然 提交于 2019-11-29 13:58:40
Mati

One way of making the changes for $.fn persistent (without editing the source) is to use expose plugin in combination with imports:

module : {
    loaders : [
            {
                test : /jquery/,
                loader : 'expose-loader?$!expose?jQuery'
            },
            {
                test : /eonasdan-bootstrap-datetimepicker/,
                loader : 'imports-loader?define=>false,exports=>false,moment=moment'
            }]
}

What exactly do these arguments for imorts loader do?

When you look at the source, it starts with

'use strict';
if (typeof define === 'function' && define.amd) {
    // AMD is used - Register as an anonymous module.
    define(['jquery', 'moment'], factory);
} else if (typeof exports === 'object') {
    module.exports = factory(require('jquery'), require('moment'));

and then proceeds

} else {
// Neither AMD nor CommonJS used. Use global variables.

This part define=>false,exports=>false prepends a piece of JavaScript that sets both define and exports (inside wrapped module definition) to false, allowing it to proceed to the "use the globals" part, which is exactly what we want. moment=moment tells it to set a variable moment equal to require('moment'), now when the datepicker tries to resolve the libraries from the globals, it reaches to the var moment=... definition. If you plan to include moment from the globals (not as npm dependency), you should omit this argument.

Found a simpler solution here: https://github.com/Eonasdan/bootstrap-datetimepicker/issues/1319

The solution:

var path = require('path');

module.exports = {
    resolve: {
        alias: {
            // Force all modules to use the same jquery version.
            'jquery': path.join(__dirname, 'node_modules/jquery/src/jquery')
        }
    }
};

you need to check #datetimepicker12 is correct or not it exist to your browser or not.press F12 and press ctrl+F and find id exist. And the other way any error jquery file missing espacially datetime picker js file on your page you get error 404 about that file.put that file at the top all js files hope your problem will be solve

Solution is in pull request on Github.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!