将外部库与angular cli一起使用 [英] Using external library with angular cli

查看:84
本文介绍了将外部库与angular cli一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的角度应用程序中安装了一个第三方模块 jsPDF .该模块可以正常工作,但在控制台中出现错误:

I have installed one 3rd party module jsPDF with my angular app. The module works perfectly but I get an error in my console:

Cannot find module '../../../node_modules/jspdf/dist/jspdf.min.js'.

我做什么:

  1. 通过npm安装模块:

npm install MrRio/jsPDF --save

  1. 将模块导入我的组件中:

import * as jsPDF from '../../../node_modules/jspdf/dist/jspdf.min.js';

  1. 然后简单地在我的组件中与此模块一起使用.

这里缺少什么吗?

推荐答案

在此处查看说明:如果jsPDF(或任何其他库)需要在全局范围内,则需要将JS文件添加到angular-cli.json文件中的apps[0].scripts中,然后WebPack将其打包,就像它是用<script>标记.如果这样做,可以通过在src/typings.d.ts或组件中添加declare var jsPDF: any;来实现.

If jsPDF (or any other library) needs to be in the global scope, you will need to add the JS file to apps[0].scripts in your angular-cli.json file, which WebPack then bundles as if it were loaded with a <script> tag. If you do that, you can get at it by adding declare var jsPDF: any; in your src/typings.d.ts or component.

但是,看起来好像有jsPDF的类型 npmjs.com/package/@types/jspdf ,因此您可以在运行npm install --save-dev @types/jspdf之后将其包括在内;您应该可以在组件中import { jsPDF } from 'jspdf';.

However, it looks like there are typings for jsPDF npmjs.com/package/@types/jspdf so you can include it after running npm install --save-dev @types/jspdf; you should be able to import { jsPDF } from 'jspdf'; in your component.

这篇关于将外部库与angular cli一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆