json - 如何在 React.js 中从 json 给出相对本地路径?
问题描述
我如何使用从 Json 到我的 React 组件渲染的本地路径,
一个<img src={this.props.imgUrl}/>
。我在互联网上搜索了答案,但没有运气:(
NOT WORKING :(
[
{
"name": "Apple",
"id": "001",
"price": "$995",
"imgUrl": "./Pictures/219.jpg"
}
]
WORKING, BUT TURTLE SPEED :/
[
{
"name": "Apple",
"id": "001",
"price": "$995",
"imgUrl": "http://www.placekitten.com/200/200"
}
]
从 'react' 类中导入 React 产品扩展 React.Component { constructor(props) { super(props) }
render() {
return (
<div>
<img src={this.props.imgUrl} />
<p>Name: {this.props.name}</p>
<p>Id: {this.props.id}</p>
<p>Price: {this.props.price}</p>
</div >
)
}
}
解决方案
试试这个,“imgUrl”:“file:/Pictures/219.jpg”而不是“imgUrl”:“./Pictures/219.jpg”
还要确保,你给出了绝对路径。
推荐阅读
- mysql - 如何将两列中的不同值计算为一个数字
- mysql - systemd[1]:安装 mysql-server 时无法启动 MySQL Community Server
- javascript - 日期范围提示模态不能用作所述模态中的变量
- ios - 如何在 iOS 中使用 swift 集成支付网关 Paytm?
- list - 如何从列表中获取素数并将它们放入空列表中
- c++ - 如何在类/对象 C++ 中使用复数
- postgresql - 为什么选择一个真实的类型只能在 Postgres 中打印出最多 6 位数字?
- laravel - 使用 Voyager admin for Laravel 在 BREAD 中为模型设置关系时出现问题
- python - 如何在 Python 中使用正则表达式创建和访问嵌套组?
- jenkins - 詹金斯,詹金斯文件管道中的转义空间字符