【问题标题】:Angular 10 : Property 'comments' does not exist on type 'typeof Dish'Angular 10:“typeof Dish”类型上不存在属性“comments”
【发布时间】:2020-10-24 09:08:53
【问题描述】:

//这是终端中的错误信息 r

在 localhost 中显示 Cannot GET /

这类似于购物车 我尝试从外部类(dishes.ts)获取数据,并通过单击购物项目它应该显示项目的描述及其视图 //这是Component.html代码

    <div class="container"
    fxLayout="row"
    fxLayout.sm="column"
    fxLayout.xs="column"
    fxLayoutAlign.gt-md="space-around center"
    fxLayoutGap="10px" 
    fxLayoutGap.xs="0">

  <div fxFlex="40">
    <mat-card *ngIf="dish">
        <mat-card-header>
            <mat-card-title>
                <h1 style="font-weight: bold"> {{dish.name | uppercase}}</h1>
            </mat-card-title>
        </mat-card-header>
        <mat-card-content>
            <img height="300px" src="{{dish.image}}" alt="{{dish.name}}">
            <h3> {{dish.description}}</h3>
        </mat-card-content>
        <mat-card-actions>
            <button mat-button >LIKE</button>
            <button mat-button >SHARE</button>
          </mat-card-actions>
</mat-card>    
  </div>
  <div fxFlex="40">
    <h1 style="font-weight: bold">Comments</h1>
    <mat-list *ngIf="dish">
        <mat-list-item *ngFor="let cm of dish.comments">  
        <h4 matLine>{{cm.comment}} </h4>
        <p matLine>{{cm.rating}} star </p>
        <p matLine>-- {{cm.author}} {{ cm.date | date}}</p>
    </mat-list-item>
    </mat-list>
  </div>
</div>

//这是Component.ts代码

import { Component, OnInit, Input } from '@angular/core';
import {Dish} from '../shared/dish';
import{DISHES} from '../shared/dishes';
@Component({
  selector: 'app-dish-details',
  templateUrl: './dish-details.component.html',
  styleUrls: ['./dish-details.component.scss']
})
export class DishDetailsComponent implements OnInit {

  dishes : Dish[]= DISHES;
  @Input()
  dish =Dish;

  constructor() { }

  ngOnInit(): void {
  }

}

我也将此添加到我的 github (https://github.com/KasunHasanga/AngularConFusion.git)

【问题讨论】:

    标签: angular typescript data-binding


    【解决方案1】:

    如果要声明类型,请使用 dish: Dish; 而不是 dish = Dish;。现在你保存到dish变量类而不是类实例;

    【讨论】:

      猜你喜欢
      • 2021-01-09
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 2020-08-19
      • 2017-11-06
      相关资源
      最近更新 更多