我正在尝试使用如下循环创建字典
var productList = []
for (var product of order.products) {
var productData = {Image: String, Description: String, Price: String};
const url: String = `<img src="${product.image}" alt="${product.title}">`;
productData['Image'] = `<img src="${product.image}" alt="${product.title}">`;
productData['Image'] = url
productData['Description'] = product.description
productData['Price'] = product.salePrice
productList.push(productData);
}
因此,我将通过联系产品中的参数来构建html img
标签作为字符串
我尝试通过更改参数类型并基于其他StackOverflow答案
但一直以来,我都遇到 productData['Image']
因此,我仍然无法修复该错误或无法了解Type 'string' is not assignable to type 'StringConstructor'
谢谢。
答案 0 :(得分:2)
您可以将匿名对象分配给变量,或者指示类型并分配值:
const productData: { Image: string, Description: string, Price: string } = { Image: '', Description: '', Price: '' };
OR
const productData = { Image: '', Description: '', Price: '' };