首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在React.js中修复json的本地路径?

如何在React.js中修复json的本地路径?
EN

Stack Overflow用户
提问于 2019-08-17 10:45:14
回答 1查看 714关注 0票数 0

如何修复从Json到React组件呈现本地路径,

我在互联网上到处寻找答案,但没有找到答案:

代码语言:javascript
复制
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 {React.Component(Props){ super(props) }

代码语言:javascript
复制
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 >
    )
}

}

代码语言:javascript
复制
EN

回答 1

Stack Overflow用户

发布于 2019-08-17 11:07:55

因为我不确定你是不是在用Webpack为你的影像服务。我假设你不是,这里是从你的public目录中提供图像的基本方法。

下面是结构:

代码语言:javascript
复制
public
|-assets
    |-pictures
       |-219.jpg
|-src
    |-myImgs.json
    |-App.js

json文件:

代码语言:javascript
复制
  {
        "name": "Apple",
        "id": "001",
        "price": "$995",
        "imgUrl": "./pictures/219.jpg"
  }

App.js

代码语言:javascript
复制
import React from 'react';
import myImgs from './myImgs.json'

class App extends React.Component {
...
render() {
  return(
   <>
     <img src={myImgs.imgUrl} alt={myImgs.name} />
   </>
  )
 };
...
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57532908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档