[英]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.