【问题标题】:Image doesn't display with MUI React使用 MUI React 不显示图像
【发布时间】:2022-01-02 00:27:46
【问题描述】:

我尝试使用 itemDate.js 文件显示我的图片:

const itemData = [
    {
      img: "../assets/photos/photoportrait.jpeg",
      title: 'Breakfast',
    },

并使用我的投资组合组件调用:

import * as React from 'react';
import ImageList from '@mui/material/ImageList';
import ImageListItem from '@mui/material/ImageListItem';
import itemData from './itemData';


function Portfolio() {

return(
<ImageList sx={{ width: 500, height: 450 }} cols={3} rowHeight={164}>
  {itemData.map((item) => (
    <ImageListItem key={item.img}>
      <img
        src={item.img}
        srcSet={item.img}
        alt={item.title}
        loading="lazy"
      />
    </ImageListItem>
  ))}
</ImageList>

没有错误消息,路径是正确的,但我的页面上没有出现任何内容。它适用于链接,但不适用于本地路径。有什么想法吗?

谢谢

【问题讨论】:

    标签: arrays reactjs image material-ui


    【解决方案1】:

    您的代码没有任何问题。我刚刚复制了您的代码并准备了codesandbox 并运行它。它工作正常。

    完整代码如下:

    import * as React from "react";
    import ImageList from "@mui/material/ImageList";
    import ImageListItem from "@mui/material/ImageListItem";
    
    const itemData = [
      {
        img: "../assets/photos/photoportrait.jpeg",
        title: "Breakfast"
      }
    ];
    
    export default function App() {
      return (
        <ImageList sx={{ width: 500, height: 450 }} cols={3} rowHeight={164}>
          {itemData.map((item) => (
            <ImageListItem key={item.img}>
              <img
                src={item.img}
                srcSet={item.img}
                alt={item.title}
                loading="lazy"
              />
            </ImageListItem>
          ))}
        </ImageList>
      );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-28
      • 1970-01-01
      • 1970-01-01
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      相关资源
      最近更新 更多