Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

typescript - Using external library with angular cli

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'.

What I did:

  1. Install the module via npm:

npm install MrRio/jsPDF --save

  1. Import the module in my component:

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

  1. Then simply works with this module in my component.

Is something missing here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Have a look at the instructions here: github.com/angular/angular-cli#3rd-party-library-installatio??n.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...