【问题标题】:Property 'app' does not exist on type 'AppComponent'“AppComponent”类型上不存在属性“app”
【发布时间】:2020-09-21 13:10:56
【问题描述】:

我不熟悉 CRUD 应用程序的 Angular 工作。目的是使用下载按钮通过角度将 mongoDB 数据下载到 Excel。在构建时,我收到此错误
src/app/app.component.html:28:26 中的错误 - 错误 TS2339 属性“app”在类型“AppComponent”上不存在。

代码
app.component.html

<div>
  <nav class="navbar navbar-expand navbar-dark bg-dark">
    <a href="#" class="navbar-brand">HOME</a> #bezKoder
    <div class="navbar-nav mr-auto">
      <li class="nav-item">
        <a routerLink="tutorials" class="nav-link">Tutorials</a>
      </li>
      <li class="nav-item">
        <a routerLink="add" class="nav-link">Add</a>
      </li>
    </div>
    
    <a style="cursor: pointer" (click)="exportexcel()">
      
    </a>
  </nav>

  <div class="container mt-3">
    <router-outlet></router-outlet>
  </div>
</div>>
<table id="excel-table"> 
  <tr>       
     <th>title</th> 
     <th>description</th>  
        
  </tr>    
  <tr *ngFor="let sup of app">
  // This is the binding part. 
     <td>{{ sup.title }}</td>  
     <td>{{ sup.description }}</td> 

  </tr> 

app.component.ts
import { Component } from '@angular/core';
import * as XLSX from 'xlsx'; 


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Angular10Crud';
  /*name of the excel-file which will be downloaded. */ 
fileName= 'tutorials_data.xlsx';  

exportexcel(): void 
    {
       /* table id is passed over here */   
       let element = document.getElementById('excel-table'); 
       const ws: XLSX.WorkSheet =XLSX.utils.table_to_sheet(element);

       /* generate workbook and add the worksheet */
       const wb: XLSX.WorkBook = XLSX.utils.book_new();
       XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');

       /* save to file */
       XLSX.writeFile(wb, this.fileName);
  
    }
}

【问题讨论】:

  • 您可能在组件的某处引用了应用程序。
  • 您没有在 component.ts 中定义应用程序属性。你只有 title 和 fileName 属性可用。

标签: angular xlsx


【解决方案1】:

错误消息是不言自明的。您在&lt;tr *ngFor="let sup of app"&gt; 中使用app,但在您的app.component.ts 中没有名为app 的属性。

你可能忘记把它放在那里了。所以只需创建app 属性(我假设它是一个数组)。

【讨论】:

    猜你喜欢
    • 2021-05-07
    • 1970-01-01
    • 2021-11-30
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多