将外部库与angular cli一起使用

编程入门 行业动态 更新时间:2024-10-10 22:19:56
本文介绍了将外部库与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'.

我做什么:

  • 通过npm安装模块:
  • npm install MrRio/jsPDF --save

  • 将模块导入我的组件中:
  • import * as jsPDF from '../../../node_modules/jspdf/dist/jspdf.min.js';

  • 然后简单地在我的组件中与此模块一起使用.
  • 这里缺少什么吗?

    推荐答案

    在此处查看说明: github/angular/angular-cli#3rd-party-library-installatio‌n .

    如果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/package/@types/jspdf ,因此您可以在运行npm install --save-dev @types/jspdf之后将其包括在内;您应该可以在组件中import { jsPDF } from 'jspdf';.

    However, it looks like there are typings for jsPDF npmjs/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一起使用

    本文发布于:2023-11-17 10:40:48,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1609764.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:angular   cli

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!