如何将图像从JSON数据导入到create-react-app

吉百利

所以我有JSON数据,其中包括位于我的公共文件夹(/ public / images)中的图像的路径。像这样

const data = [
{
    "key": 1,
    "name": "Goal Squad",
    "techs": ["React & Redux", "Express", "MySQL"],
    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et",
    "image": "../public/images/test.jpg"
},
{
    "key": 2,
    "name": "WesterosCraft",
    "techs": ["React & Redux", "Express", "MySQL"],
    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et",
    "image": "../public/images/test.jpg"
},

基于阅读其他一些类似情况,这就是我在组件中尝试过的内容;

class Card extends Component  {

render() {
    const { name, description, image } = this.props;
    const items = Object.values(this.props.techs);

    console.log(this.props)

    return (
        <CardWrapper>
            <Row>
                <Column colmd6 colsm12>
                    <Header card>{name}</Header>
                    <TechList>{items.map(tech =>
                        tech
                    ).join(' / ')}</TechList>
                    <Text regular>{description}</Text>
                    <Button>Visit Website</Button>
                </Column>

                <Column colmd6 colsm12>
                    <img src={require(`${image}`)} alt={name}/>
                </Column>
            </Row>
        </CardWrapper>
    )
}

但是create-react-app会引发错误“错误:找不到模块'../public/images/test.jpg'

有什么提示吗?

sdkcy

我们不能动态使用require。您可以像这样更改数据。

const data = [
    {
        "key": 1,
        "name": "Goal Squad",
        "techs": ["React & Redux", "Express", "MySQL"],
        "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et",
        "image": require("../public/images/test.jpg")
    },
    {
        "key": 2,
        "name": "WesterosCraft",
        "techs": ["React & Redux", "Express", "MySQL"],
        "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et",
        "image": require("../public/images/test.jpg")
    }
]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将React App作为一个组件从GitHub导入到新的React App?

如何使用ES6语法将Firebase Firestore导入到create-react-app项目中

如何将整个SVG图像文件夹导入(或如何动态加载)到React Web App中?

如何将Cocoapod导入到App Extension中?

如何将外部数据获取api导入到react componentDidMount方法中

如何将React应用(create-react-app)部署到Back4App?

如何将文件导入使用 create react app 作为原始文本的 react 应用程序?

Node + create-react-app + Express - 如何将函数导入 Express

如何将音频导入我的 Create-React-App 应用程序?

如何将插件导入 Leaflet React App

如何将Firebase导入到app模块以外的模块中?

如何将模型从models.py导入到app.py中?

如何将create-react-app部署到Google Cloud

如何将 create-react-app 部署到 gh-pages 子文件夹?

如何将 React App 部署到 GoDaddy 域

如何将create-react-app转换为Preact?

如何使用Create React App在React中导入图像(SVG或PNG)

将React App与React Native App合并

如何将参数从一个 React 组件导入到另一个组件

React App不显示本地导入的图像

我们如何使用Azure Logic App将数据从Sharpoint上载的CSV文件导入到CRM实体中?

如何对未使用的导入进行create-react-app检查

React create app-如何导入嵌套组件?

如何将使用create-react-app构建的React项目升级到下一个create-react-app版本?

React-如何将本地图像导入/来源到对象

使用React和create-react-app动态导入

使用Create React App进行React样式

如何将状态从Hook传递到React中的App Hook中显示?

如何将基于react.js的SPA部署到Azure App Service + CDN?