简体   繁体   English

如何将 save-svg-as-png 与 angular 6 一起使用

[英]How to use save-svg-as-png with angular 6

I am trying to use the save-svg-as-png library with my angular 7 project.我正在尝试在我的 angular 7 项目中使用save-svg-as-png库。

However, I cannot manage to get it to work.但是,我无法让它发挥作用。 I have installed the library using我已经使用安装了库

npm install --save save-svg-as-png

and I can see the library under node_modules.我可以在 node_modules 下看到库。

Unfortunately, the library is an old-style javascript library and I do not know what I need to do in order to have access to it in my typescript components.不幸的是,该库是一个旧式的 javascript 库,我不知道我需要做什么才能在我的打字稿组件中访问它。

The readme refers to the Typings library for which type definitions apparently exist.自述文件引用了显然存在类型定义Typings库。 However, the Typings page mentions that it is deprecated for TypeScript 2, so I did not pursue this.但是,Typings 页面提到它在 TypeScript 2 中已被弃用,所以我没有追求这个。

Apparently there is @types/save-svg-as-png for native Angular 2+ support, but when I try to install it with显然有@types/save-svg-as-png支持原生 Angular 2+,但是当我尝试安装它时

npm install --save @types/save-svg-as-png

the library cannot be found ( npm ERR! code 404 ).找不到库( npm ERR! code 404 )。

I googled some more and came across this issue on github where somebody has apparently got it working with Angular 2 by including it in the angular-cli.js file, but with the changes to Angular, this file no longer exists in version 7 and I do not know how that would need to be done nowadays.我用谷歌搜索了更多并在 github上遇到了这个问题,有人显然已经通过将它包含在 angular-cli.js 文件中来让它与 Angular 2 一起工作,但是随着对 Angular 的更改,这个文件在版本 7 中不再存在,我不知道现在需要怎么做。

I've also found the following article on how to integrate javascript libraries into Angular 2+ , but most of it relies on @types being available (which they are not, see above) and only has a brief section on how to supply your own typings.d.ts file but after playing around for quite a while, I did not get any further.我还发现了以下关于如何将 javascript 库集成到 Angular 2+ 中的文章,但其中大部分依赖于 @types 可用(它们不是,见上文)并且只有一个简短的部分关于如何提供你自己的库typings.d.ts文件,但在玩了很长一段时间后,我没有进一步了解。 Is there a more detailed explanation showing how to use this approach?是否有更详细的解释说明如何使用这种方法?

I've also found this article on stackoverflow on how to integrate IIFE based libraries into typescript apps but did not get it working.我还在stackoverflow 上找到了这篇关于如何将基于 IIFE 的库集成到打字稿应用程序中的文章,但没有让它工作。

I've added the following line to my index.html file我已将以下行添加到我的index.html文件中

<script type="javascript" src="node_modules/save-svg-as-png/lib/saveSvgAsPng.js"></script>

but how do I now access the functions provided by the library?但是我现在如何访问库提供的功能? If I understood things correctly, they should now be available on the window object, but that does not seem to be the case.如果我理解正确,它们现在应该可以在 window 对象上使用,但情况似乎并非如此。

I've also read this stackoverflow question on how to use non typescript libraries but one of my problems is that I don't even know into what namespace save-svg-as-png is being imported.我还阅读了有关如何使用非打字稿库的这个stackoverflow 问题,但我的问题之一是我什至不知道将save-svg-as-png导入到哪个命名空间中。

Has anyone managed to get this library working with an Angular 6/7 project and could give a detailed explanation on all steps required?有没有人设法让这个库与 Angular 6/7 项目一起工作,并且可以详细解释所需的所有步骤?

I'll try to summarise the solution as suggested by Hypenate:我将尝试按照 Hypenate 的建议总结解决方案:

Install the library:安装库:

npm install --save save-svg-as-png

At the top of my typescript file/angular component:在我的打字稿文件/角度组件的顶部:

import * as svg from 'save-svg-as-png';

Using it in my angular component:在我的角度组件中使用它:

svg.svgAsPngUri(document.getElementById('idOfMySvgGraphic'), {}, (uri) => {
      console.log('png base 64 encoded', uri);
    });

All exported functions are available on svg .所有导出的函数都可以在svg

Also, we can use the saveSvgAsPng.js file as an external js file instead of installing that package此外,我们可以使用 saveSvgAsPng.js 文件作为外部 js 文件,而不是安装该包

  1. Add saveSvgAsPng.js file into src/assets/js将 saveSvgAsPng.js 文件添加到 src/assets/js
  2. And add this JavaScript file in scripts array in angular.json file并将此 JavaScript 文件添加到 angular.json 文件的脚本数组中

"scripts": [ "src/assets/js/saveSvgAsPng.js" ]

  1. declare saveSvgAsPng in your component.ts在你的 component.ts 中声明 saveSvgAsPng

declare const saveSvgAsPng: any;

  1. And call that when you need to download.并在需要下载时调用它。

saveSVG(): void{ saveSvgAsPng(document.getElementById('id'), fileName); }

Make sure to restart your angular app ( ng serve ) if you change scripts array or declared name如果您更改脚本数组或声明的名称,请确保重新启动您的 angular 应用程序( ng serve

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM