gpt4 book ai didi

javascript - 我如何错误地导出对象?

转载 作者:行者123 更新时间:2023-12-01 00:27:44 25 4
gpt4 key购买 nike

我想知道我如何错误地导出对象?在我看来,我看到了这个错误:./src/context.js
尝试导入错误:“detailProduct”未从“./data”导出。

在控制台中,我确实看到对象已正确填充,但由于某种原因,由于上述错误,它不会呈现我的 View 。我做错了什么?

export const storeProducts = [
{
id: 1,
title: "Crusk Beanie (black)",
img: "img/CruskipBlackBeanie.png",
price: 1,
company: "Cruskip",
info:
"Winter's right around the corner, get your beanie today!",
inCart: false,
count: 0,
total: 0
},
{
id: 3,
title: "Cruskip Short Sleeve T-shirt",
img: "img/CruskipWhiteShortSleeve.jpg",
price: 8,
company: "Cruskip",
info:
"Exclusive Cruskip white t-shirts!",
inCart: false,
count: 0,
total: 0
},
];

let detailProduct = {};

storeProducts.forEach((arrayItem) => {
detailProduct = {
id: arrayItem.id,
title: arrayItem.title,
img: arrayItem.img,
price: arrayItem.price,
company: arrayItem.company,
info: arrayItem.info,
inCart: arrayItem.inCart,
count: arrayItem.count,
total: arrayItem.total
};
console.log(arrayItem);
});

export default detailProduct;

最佳答案

您可能会使用大括号导入它,但对于 default 导出则不能这样做:

// This will work
import detailProduct from './data';

// This won't
import { detailProduct } from '.data';

另一方面,由于 storeProducts 是命名导出,因此它的工作方式相反:

// This will work
import { storeProducts } from './data';
// This won't
import storeProducts from './data';

关于javascript - 我如何错误地导出对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58848289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com