简体   繁体   English

Angular 类型“ExpenseEntryComponent”上不存在属性“expenseEntry”

[英]Angular Property 'expenseEntry' does not exist on type 'ExpenseEntryComponent'

  1. I am trying to build expense entry app from this tutorial https://www.tutorialspoint.com/angular8/angular8_pdf_version.htm page 33.我正在尝试从本教程https://www.tutorialspoint.com/angular8/angular8_pdf_version.htm第 33 页构建费用输入应用程序。

I am getting this error - Property 'expenseEntry' does not exist on type 'ExpenseEntryComponent'.我收到此错误 - 类型“ExpenseEntryComponent”上不存在属性“expenseEntry”。 i have tried this links我试过这个链接

a) Angular error TS2339 Property does not exist on type a) Angular 错误 TS2339 类型上不存在属性

b) Angular - How to fix 'property does not exist on type' error? b) Angular - 如何修复“类型上不存在属性”错误?

but i didnt get it clearly但我没明白

  1. my expense-entry.ts file我的费用条目.ts 文件

 import { Component } from "@angular/core"; import { OnInit } from "@angular/core"; export interface ExpenseEntry { id: number; item: string; amount: number; category: string; location: string; spendOn: Date; createdOn: Date; } @Component({ template: '' }) export class ExpenseEntryComponent implements OnInit{ title: string; expenseEntry: ExpenseEntry; constructor(){ } ngOnInit(){ this.title = "Expense Entry"; this.expenseEntry = { id: 1, item: "Pizza", amount: 21, category: "Food", location: "Zomato", spendOn: new Date(2020, 6, 1, 10, 10, 10), createdOn: new Date(2020, 6, 1, 10, 10, 10), }; } }

  1. expense-entry.component.ts file is here费用入口.component.ts 文件在这里

 import { Component, OnInit } from '@angular/core'; import {ExpenseEntry} from '../../app/expense-entry/expense-entry' @Component({ selector: 'app-expense-entry', templateUrl: './expense-entry.component.html', styleUrls: ['./expense-entry.component.css'] }) export class ExpenseEntryComponent implements OnInit { title: string | undefined; constructor() { } ngOnInit(): void { this.title = "Expense Entry"; } }

  1. my expense-entry.component.html file我的费用条目.component.html 文件

 <:------------------content-------> <div class="container"> <div class="row"> <div class="col-lg-12 text-center" style="padding-top; 20px:"> <div class="container" style="padding-left; 0px: padding-right;0px:"> <div class="row"> <div class="col-sm" style="text-align; left:">{{title}} </div> <div class="col-sm" style="text-align; right:"> <button type="button" class="btn btn-primary">Edit</button> </div> </div> </div> <div class="container box" style="margin-top; 10px:"> <div class="row"> <div class="col-2" style="text-align; right:"> <strong><em>Item:</em></strong></div> <div class="col" style="text-align; left.">{{expenseEntry:item}}</div> </div> <div class="row"> <div class="col-2" style="text-align; right:"> <strong><em>Amount:</em></strong></div> <div class="col" style="text-align; left.">{{expenseEntry:amount}}</div> </div> <div class="row"> <div class="col-2" style="text-align; right:"> <strong><em>Category:</em></strong></div> <div class="col" style="text-align; left:"> food</div> </div> <div class="row"> <div class="col-2" style="text-align; right:"> <strong><em>Location:</em></strong></div> <div class="col" style="text-align; left.">{{expenseEntry:location}}</div> </div> <div class="row"> <div class="col-2" style="text-align; right:"> <strong><em>Spend on:</em></strong></div> <div class="col" style="text-align; left.">{{expenseEntry.spendOn}} </div> </div> </div> </div> </div> </div>

  1. when i insert {{expenseentry.item}} it shows error.当我插入 {{expenseentry.item}} 时,它显示错误。 i tried restarting the server but didnt work我尝试重新启动服务器但没有工作

expense-entry.ts will only export interface. fee-entry.ts 只会导出接口。 You have created two components with same name.您已经创建了两个具有相同名称的组件。

expense-entry.ts:费用条目.ts:

export interface ExpenseEntry { 
   id: number; 
   item: string; 
   amount: number; 
   category: string; 
   location: string; 
   spendOn: Date; 
   createdOn: Date; 
}

Then in your ExpenseEntryComponent.ts you need to import above interface as below:然后在您的 ExpenseEntryComponent.ts 中,您需要导入上面的接口,如下所示:

import { ExpenseEntry } from '../expense-entry';

@Component({
  selector: 'app-expense-entry',
  templateUrl: './expense-entry.component.html',
  styleUrls: ['./expense-entry.component.css']
})

export class ExpenseEntryComponent implements OnInit { 

   title: string; 
   expenseEntry: ExpenseEntry; 
   constructor() { } 

   ngOnInit() { 
      this.title = "Expense Entry"; 
      this.expenseEntry = { 
         id: 1, 
         item: "Pizza", 
         amount: 21, 
         category: "Food", 
         location: "Zomato", 
         spendOn: new Date(2020, 6, 1, 10, 10, 10), createdOn: new Date(2020, 6, 1, 10, 10, 10), 
      }; 
   } 
}

With a quick scrub through your code, I can see the ExpenseEntryComponent class is used in two files and the expense-entry.component.ts file doesn't have any member called expenseEntry in the class file, but expense-entry.ts does.通过快速浏览您的代码,我可以看到ExpenseEntryComponent class 在两个文件中使用,并且在 class 文件中, expense-entry.component.ts文件没有任何名为expenseEntry的成员,但expense-entry.ts有。

Try removing/altering that and your code will work.尝试删除/更改它,您的代码将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM