How to use Snap.svg with Angular v4.0

痴心易碎 提交于 2019-12-03 00:24:15

First install snapsvg and its types:

npm install --save snapsvg
npm install --save @types/snapsvg

Secondly in .angular-cli.json add snap.svg-min.js to scripts:

"scripts": [
  "../node_modules/snapsvg/dist/snap.svg-min.js"
]

Now at the top of your component and just after the imports:

declare var Snap: any;
declare var mina: any;    // if you want to use animations of course

Then:

ngOnInit() {
  const s = Snap('#my-svg');
  const c = s.circle(50, 50, 100);
}

Currently there seems to be a bug with using SnapSVG with WebPack (which angular-cli uses). So far the only way I've got this to work is by including the snapsvg.js file as a script in angular-cli.json . Add this to the scripts array as below:

"scripts": [ "node_modules/snapsvg/dist/snap.svg.js"], 

You can try using snapsvg-cjs - SnapSVG in CommonJS (for Webpack). Simple googling got me to this demo https://github.com/stevefarwell/angular2-snapsvg-demo (it uses cli and Angular 4)

1) Install imports-loader and snap:

npm i imports-loader

2) Use imports-load to import:

import Snap from 'imports-loader?this=>window,fix=>module.exports=0!snapsvg/dist/snap.svg.js';
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!