首页 > 解决方案 > 无法读取 PDF 组件中未定义的属性“forEach”

问题描述

我收到以下错误,见下文:我做错了什么?

ERROR TypeError: Cannot read property 'forEach' of undefined
    at Object.createModels (vendor.js:117599)
    at Object.jsPDF.API.autoTable (vendor.js:116203)
    at PDFComponent.push../src/app/Shared/Directives/Download/pdf-generator.component.ts.PDFComponent.downloadPDF (main.js:344)
    at Object.eval [as handleEvent] (ng:///AppModule/PDFComponent.ngfactory.js:13)
    at handleEvent (vendor.js:74287)
    at callWithDebugContext (vendor.js:75796)
    at Object.debugHandleEvent [as handleEvent] (vendor.js:75383)
    at dispatchEvent (vendor.js:70702)
    at vendor.js:71327
    at HTMLButtonElement.<anonymous> (vendor.js:95474)

下面的代码

import { Component, Input, EventEmitter, Output,Inject  } from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {AuthService} from '../../Services/auth.service';
import * as jsPDF from 'jspdf';
import 'jspdf-autotable';

declare var jsPDF: any; // Important
@Component({
    selector: 'pdf-download',
    templateUrl: './pdf-generator.component.html',

})
export class PDFComponent   {
    @Input() downloadData: any; 
    //@Input() columns: any; 
    @Input() columns: any[] = []; 
    @Input() reportName: string; 
    constructor(private authService: AuthService) { }
    // public columns=[];


    downloadPDF(){


 for(var key in  this.downloadData[0])
         {

            this.columns.push({title:key,dataKey:key})
        }
}

标签: javascriptarraysangulartypescript

解决方案


我认为你如何为每个循环尝试使用以下对象数组是一个问题。它可以帮助你

例如:

var downloadData=[{item:1,value:"one"},{item:2,value:"two"},{item:3,value:"three"}]

  var   columns=[]
     for(var key in  downloadData)
             {
    
                columns.push({title:downloadData[key].item,dataKey:downloadData[key].value})
            }
            
            console.log(columns)


推荐阅读