繁体   English   中英

'typeof Foods'类型不存在属性'id'

[英]Property 'id' does not exist on type 'typeof Foods'

查看下面的代码,我试图使用复选框从我的数据表中删除客户端。 当我勾选一个复选框时,我在控制台中看到对象的属性已从数据库中获取。 我试图通过id删除但终端引发此错误“属性'id''在类型'typeof Clients'上不存在”。 我搜索后还没有找到任何解决方案。 有帮助吗?

// Http服务

@Injectable()

 export class HttpService {

  constructor(private http:Http) {

  }



  deleteFood(food) {
    return this.http.delete('http://localhost:9000/api/v1/food/${id}')
      .map((response:Response) => response.json())
  }
}

//表

 <tbody>
    <tr *ngFor="let food of Foods; let i = index">
      <td><input #{{food.id}} [(ngModel)]="food.selected" type="checkbox" (change)="checkbox(food)"></td>

      <td>{{client.type}}</td>
      <td>{{client.location}}</td>

    </tr>

  </tbody>
</table>

<button type="button" (click)="deleteFood()">Delete</button>

//零件

  export class FoodComponent {

  Foods : Foods[] = []; 



  constructor(private httpService: HttpService, private router: Router){



   selectedFood = Foods;

   deleteFood(){
    this.httpService.deleteFood(this.selectedFood.id)
     .subscribe(data =>{
       console.log(data)
     })
  }

  checkbox(food){
    food.selected = (food.selected) ? false: true;
    this.selectedFood = food;
    console.log(this.selectedFood);
  }
}

//Food.ts

  export class Food{

  constructor(
    public type:string,
    public location: string,

 ){}
}

在组件中,您将Clients类型分配给activeClient属性,而不是定义其类型。 并且您的Clients类没有静态属性id ,这就是编译器在您访问this.activeClient.id时抱怨的原因。

如果你改变:

activeClient = Clients;

至:

activeClient: Clients;

你不应该再遇到问题了。

暂无
暂无

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

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