$(…).datetimepicker is not a function

前端 未结 4 768
情歌与酒
情歌与酒 2020-12-20 16:10

I use webpack and want to use bootstrap-datetimepicker. In my webpack config I use ProvidePlugin to get \"jquery module\".

In my code I get error $(.

相关标签:
4条回答
  • 2020-12-20 16:35

    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.

    0 讨论(0)
  • 2020-12-20 16:37

    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')
            }
        }
    };
    
    0 讨论(0)
  • 2020-12-20 16:43

    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

    0 讨论(0)
  • 2020-12-20 16:55

    Solution is in pull request on Github.

    0 讨论(0)
提交回复
热议问题