【发布时间】:2020-07-27 14:27:27
【问题描述】:
我正在尝试使用以下代码设置 items 数组并推送到 localStorage:
public onSubmit(thumbnail, quantity, product_name, product_price){
const data = {
thumbnail,
quantity,
product_name,
product_price
};
localStorage.setItem(this.items, JSON.stringify(data));
this.items.push(JSON.parse(localStorage.getItem(data)));
}
我收到以下错误:
“any[]”类型的参数不能分配给“string”类型的参数。
for localStorage.setItem(this.items, JSON.stringify(data));
类型参数 '{ thumbnail: any;数量:任意;产品名称:任何;产品价格:任何; }' 不可分配给“字符串”类型的参数。
for this.items.push(JSON.parse(localStorage.getItem(data)));
在我的 HTML 中:
<tr *ngFor="let item of items;">
<td>
<td><img src="../assets/images/gallery/{{item.thumbnail}}" /></td>
<td>{{item.quantity}} </td>
<td>{{item.product_name }}</td>
<td>{{item.product_price}} </td>
<td>{{item.quantity * item.product_price }}</td>
</tr>
【问题讨论】:
-
setItem()键参数的类型必须为stringdeveloper.mozilla.org/en-US/docs/Web/API/Storage/setItem -
在函数之外我有 items = [];
标签: angular