How to use javascript functions in an Angular 2 component from a different file

后端 未结 4 1569
后悔当初
后悔当初 2020-12-15 20:14

I have a javascript file that contains some data manipulation functions (No DOM manipulation at all) such as float rounding, mathematical operations, ...etc

my js fi

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 20:55

    this is what it worked for me I was trying to use html2pdf from an Angular2 app, so I had to make a reference to this function

    var html2pdf = (function(html2canvas, jsPDF) {
    

    declared in html2pdf.js.

    So I added just after the import declarations in my angular-controller this declaration:

    declare function html2pdf(html2canvas, jsPDF): any;
    

    then, from a method of my angular controller I'm calling this function:

    generate_pdf(){
        this.someService.loadContent().subscribe(
          pdfContent => {
            html2pdf(pdfContent, {
              margin:       1,
              filename:     'myfile.pdf',
              image:        { type: 'jpeg', quality: 0.98 },
              html2canvas:  { dpi: 192, letterRendering: true },
              jsPDF:        { unit: 'in', format: 'A4', orientation: 'portrait' }
            });
          }
        );
      }
    

    Hope it helps

提交回复
热议问题