使用带有 angular cli 的外部库 [英] Using external library with angular cli
问题描述
我已经在我的 angular 应用程序中安装了一个 3rd 方模块 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:
找不到模块'../../../node_modules/jspdf/dist/jspdf.min.js'
.
我做了什么:
- 通过 npm 安装模块:
npm install MrRio/jsPDF --save
- 在我的组件中导入模块:
import * as jsPDF from '../../../node_modules/jspdf/dist/jspdf.min.js';
- 然后在我的组件中简单地使用这个模块.
这里有什么遗漏吗?
推荐答案
看看这里的说明:github.com/angular/angular-cli#3rd-party-library-installatio n.
如果 jsPDF(或任何其他库)需要在全局范围内,您需要将 JS 文件添加到 angular-cli 中的
文件,然后 WebPack 将其打包,就像加载了 apps[0].scripts
.json 标签一样.如果你这样做,你可以通过在你的
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屋!