简体   繁体   English

在 angular 中获取数据时出现错误

[英]Im getting error while fetching data in angular

TS2345: Argument of type (id: number; name: string; price: mumberg Storage: number; ) is not assignable to parameter of type 'null'. TS2345:类型参数(id:数字;名称:字符串;价格:mumberg 存储:数字;)不可分配给“null”类型的参数。

<button class="btn btn-primary" (click)-"openForm(mobile)">Edit</button>

app.component.html app.component.html

<tr *ngFor="let mobile of mobiles">
   <td>{{ mobile.id }}</td>
   <td>{{ mobile.name}}</td>
   <td>{{ mobile.price }}</td> <td>{{ mobile.ram }}</td>
   <td>((mobile.Storage }}</td> 
<td>

<button class="btn tn-primary" (click)="openForm (mobile)">Edit</

App.component.ts应用程序组件.ts

If(data){
  this.mobileId=data.id;
  this.mobileName=data.name;
  this.mobileprice=data.price:
  this.mobileRam=data.ram;
  this.mobileStorage=data.Storage;
}

You have to check the parameter type of the function and make ensure that is in the correct type.您必须检查 function 的参数类型并确保其类型正确。 If it is supposed to be an object, ensure that the properties of the object match the expected properties in the function's definition you can also try to define the type of data as data: any .如果它应该是 object,请确保 object 的属性与函数定义中的预期属性相匹配,您还可以尝试将数据类型定义为data: any

The error message is saying that the argument provided to the function openForm is not assignable to the parameter, which is expected to be of type null.错误消息是说提供给 function openForm 的参数不可分配给参数,该参数应为 null 类型。

In the HTML code, there is a typo in the button's click event. HTML代码中,按钮的点击事件有错别字。 It has a hyphen ((click)-) instead of an equal sign ((click)).它有一个连字符 ((click)-) 而不是等号 ((click))。 Also, the closing tag for the button is missing the > character.此外,按钮的结束标记缺少 > 字符。

In the TypeScript code, there is a syntax error in the if statement. TypeScript代码中,if语句存在语法错误。 There is a colon (:) at the end of the line this.mobileprice=data.price:, which is not valid syntax. this.mobileprice=data.price: 行末尾有一个冒号 (:),这是无效语法。

Here's the corrected code:这是更正后的代码:

app.component.html:应用程序组件.html:

<button class="btn btn-primary" (click)="openForm(mobile)">Edit</button>

app.component.ts:应用程序组件.ts:

if(data){
  this.mobileId=data.id;
  this.mobileName=data.name;
  this.mobileprice=data.price;
  this.mobileRam=data.ram;
  this.mobileStorage=data.Storage;
}

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

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