reactjs 有没有一种方法可以在React JSX文件中导入图像而不定义名称?

rn0zuynd  于 12个月前  发布在  React
关注(0)|答案(1)|浏览(97)

我尝试使用ReactJS创建一个菜单网站。我有一些组件,他们都工作正常,但我唯一的问题是,我的图像不显示。我有一系列的数据,我想为他们使用图像。它们并不像我们说的那样,

import React from "react";

const FoodMenus = [
 {
    name: "Pizza Margherita",
    ingredients: "Tomato and mozarella",
    price: 10,
    photo: "../assets/margherita.jpg",
    soldOut: false,
  },
...
]

我尝试了所有的方法导入图像一样;

import React from "react";

const FoodMenus = [
 {
    name: "Pizza Margherita",
    ingredients: "Tomato and mozarella",
    price: 10,
    photo: "/src/assets/focaccia.jpg",
    soldOut: false,
  },
...
]

这个也不管用。

nfeuvbwi

nfeuvbwi1#

你可以试试这个

import React from "react";
    import photo1 from "../assets/margherita.jpg";
    const FoodMenus = [
    {
     name: "Pizza Margherita",
     ingredients: "Tomato and mozarella",
     price: 10,
     photo: photo1,
     soldOut: false,
    },
    ...
    ]

相关问题