Reactjs 我希望itemslist中的每个项目都有与其匹配的图像
我只在“Lenovo item”上尝试了这一点,但没有渲染图像。我将图像保存在源文件夹中,并将其导入到Reactjs 我希望itemslist中的每个项目都有与其匹配的图像,reactjs,Reactjs,我只在“Lenovo item”上尝试了这一点,但没有渲染图像。我将图像保存在源文件夹中,并将其导入到App.js组件中 class ItemsList extends React.Component { constructor(props) { super(props); this.state = {total:0, items: [ {image:{image},name:"Lenovo",descr
App.js
组件中
class ItemsList extends React.Component {
constructor(props) {
super(props);
this.state = {total:0,
items: [
{image:{image},name:"Lenovo",description:"Core i5 - 4gb ram - 256gb SSD", price: 540},
{name:"Dell",description:"Core i7 - 8gb ram - 1tb HDD", price: 700},
{name:"Asus",description:"Core i3 - 4gb ram - 512gb HDD", price: 429}
]}
1) 在ItemList组件中(我已导入位于src文件夹中的'logo.svg'图像)
2) 在ItemList组件的渲染功能中,我添加了图像道具
render() {
var theThis = this;
var items = this.state.items.map(function(item)
{
return(
<Item name={item.name} description={item.description} price={item.price} image={item.image}
handleShow={theThis.showDetails}
handleTotal={theThis.calculate}
/>
);
});
render(){
var theThis=这个;
var items=this.state.items.map(函数(项)
{
返回(
);
});
3) 在Item组件的render函数中,我使用了img标记来渲染图像
render() {
return (
<div>
<h4>{this.props.name}</h4>
<p>{this.props.description}</p>
<img src={this.props.image} height={50} width={50}></img>
<button onClick={this.buy}>Buy</button>
<button onClick={this.show}>Show</button>
<span> ${this.props.price}</span>
<span> (Cart: {this.state.amount} items.)</span>
<hr/>
</div>
);
}
render(){
返回(
{this.props.name}
{this.props.description}
购买
显示
${this.props.price}
(购物车:{this.state.amount}项。)
);
}
我尝试要求提供图像,但没有渲染。无法在itemsList组件顶部导入图像,因为导入仅在顶层工作。图像与App.js组件一样位于源文件夹中。{image:{require('./image.jpg')},名称:“Lenovo”,说明:“Core i5-4gb ram-256gb SSD”,价格:540},你能显示你的文件夹结构吗(显示你的图片所在的位置和项目列表组件)?,我能给你一个项目的副本吗?我希望所有项目都有一个匹配的图片
render() {
return (
<div>
<h4>{this.props.name}</h4>
<p>{this.props.description}</p>
<img src={this.props.image} height={50} width={50}></img>
<button onClick={this.buy}>Buy</button>
<button onClick={this.show}>Show</button>
<span> ${this.props.price}</span>
<span> (Cart: {this.state.amount} items.)</span>
<hr/>
</div>
);
}