vue.jssnap.svg

Snap.svg with Vue.js


I'm trying to use Snap.svg in my Vue.js app, however I'm confuse with how to do so. I used Vue CLI 3 command to initialized my project, and install the snapsvg dependency with yarn.

Also, I read this article. But I can't find the webpack.base.conf.js file !

When I try to import the dependency into my main.js file or into any component I've got no error but my App become empty.

What did I miss to properly import Snap.svg in my Vue.js app ?


Solution

  • In Vue cli 3, webpack is configured in vue.config.js

    To use snap place the vue.config.js with the following content in your root directory

    module.exports = {
      chainWebpack: config => {
        config.module
          .rule("snapsvg")
          .test(require.resolve("snapsvg"))
          .use("imports-loader?this=>window,fix=>module.exports=0")
          .loader("imports-loader")
          .end();
      }
    };
    

    add this line to your main.js file:

    const snap = require(`imports-loader?this=>window,fix=>module.exports=0!snapsvg/dist/snap.svg.js`);