search

Home  >  Q&A  >  body text

javascript - JS library conversion problem from script reference to using import

Maybe the title is not very clear, I will explain in detail:

I rely on a jq component in a project. It is referenced through a script tag. How can I make it available through the import method?

https://www.npmjs.com/package/particles.js

This is the library above. I changed the part where I forgot to hang a function on the window to export default. I found that an error will be reported. How can I modify this so that I can use import?

window.particlesJS.load = function(tag_id, path_config_json, callback){
    code ...
}

//这里我加了

export defalut particlesJS;

I found that it is not possible. What did I do wrong?

ringa_leeringa_lee2847 days ago577

reply all(1)I'll reply

  • 阿神

    阿神2017-05-19 10:49:36

    Install into node_module

    npm install particles.js

    Quote (in entry file)

    import 'particles.js'

    If this jq component needs to call jq methods or use jq objects, jq must also be installed and exposed to the world

    npm install jquery@xxx

    Exposed to the overall situation, I use webpack here, and the rest is up to you - -

            new webpack.ProvidePlugin({
                $: 'jquery',
                jQuery: 'jquery',
                'window.jQuery': 'jquery',
                'window.$': 'jquery'
            })

    reply
    0
  • Cancelreply