本文介绍了如何在angular-cli上使用外部库? (pdfmake)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,因此,我在我的测试项目中使用了angular-cli,并且由于我是Web开发领域的新手,因此我尝试了新的想法.这次我无法正常工作.

Ok, so, im using angular-cli for my test project and since im new to web development world im trying new thinks. This time I couldn't make it work.

好吧,我尝试了什么?

好吧,我顺应pdfmake自述文件并将其放入index.html中,并将这两行放在body标签内:

well I kind followed pdfmake readme and into my index.html I placed this two lines inside body tag:

<script src='build/pdfmake.min.js'></script>
<script src='build/vfs_fonts.js'></script>

好吧,我收到此错误:

因此,我带走了,然后只将pdfmake导入了我的组件,就像这样:

So, I took than away, and into my component I only imported pdfmake, like this:

import * as pdfmake from 'pdfmake/build/pdfmake.js';

及其作用是什么?好吧,什么都没有,我现在仍然没有在代码中的任何地方调用它,

and what it does? well, nothing, i still didn't called it any where in my code, doing now:

pdfmake.createPdf(this.docDefinition).download();

pdfmake.createPdf(this.docDefinition).download();

docDefinition代表我的pdf内容.

docDefinition stands for my pdf content.

好吧,现在呢?出现此错误:

ok, and now? This error appears:

好吧,我可能需要导入vfs_font,在哪里?好吧,我不知道,我尝试在scripts标签处导入angular-cli.json,不,不起作用,甚至尝试在我的组件中,nope导入.

ok, I probably need to import vfs_font, where? well, I have no idea, i tried importing in angular-cli.json at scripts tag, nope, dont work, I even tried importing at my component, nope.

我尝试使用在nt npm网站上找到的ng-pdf-make库,但这根本不起作用.

I tried using ng-pdf-make library found ant npm site, well it doesn't work at all.

我可能正在犯一个新手错误,对不起,如果它简单又愚蠢:P.

Im probably making an novice mistake, sorry if its something easy and dumb :P.

@编辑

我尝试了与他所做的相同的事情,实际上它在我的项目中可以与jquery一起使用,但不适用于pdfmake.

I tried the same thing he did, and in fact it works with jquery in my project, but it doesn't work for pdfmake.

这是我的angular-cli.json脚本标签:

This is my angular-cli.json script tag:

"scripts": [
        "../node_modules/pdfmake/build/pdfmake.min.js",
        "../node_modules/pdfmake/build/vfs_fonts.js",
        "../node_modules/jquery/dist/jquery.js",
        "../node_modules/tether/dist/js/tether.js",
        "../node_modules/bootstrap/dist/js/bootstrap.js"
      ],

存在一个事实,即使使用vfs_fonts import,我也无法使用pdfmake.createPdf("content"):

and there is the fact that I cant use pdfmake.createPdf("content") even with the vfs_fonts import:

import * as pdfmake from 'pdfmake/build/pdfmake.min.js';
import * as pdfFonts from 'pdfmake/build/vfs_fonts.js';

推荐答案

首先,您需要通过npm安装pdfmake

First you need install your pdfmake through npm

npm install pdfmake --save

它将把您的包裹保存在node_modules

it will save your package in node_modules

之后,您可以像这样访问pdfmake

after that you can access pdfmake like this

 import * as pdfMake from 'pdfmake/build/pdfmake';
 import * as pdfFonts from 'pdfmake/build/vfs_fonts';

在使用pdfMake进行初始化之前

before using pdfMake initialize like this

 pdfMake.vfs = pdfFonts.pdfMake.vfs;

示例:-

import { Component } from '@angular/core';
 import * as pdfMake from 'pdfmake/build/pdfmake';
 import * as pdfFonts from 'pdfmake/build/vfs_fonts';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';
constructor(){
     //called first time before the ngOnInit()
     pdfMake.vfs = pdfFonts.pdfMake.vfs;
      var dd = { content: 'your pdf data' };
    pdfMake.createPdf(dd).download();
  }

}

它应该工作.

您不需要在index.html或脚本中添加任何内容.

you don't need add any thing in the index.html or scripts.

这篇关于如何在angular-cli上使用外部库? (pdfmake)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 18:33