在React中不显示服务器上的图像

问题描述 投票:0回答:1

这是代码

import React from "react";
import ReactDOM from "react-dom/client";

const Header = () => {
  return (
    <div className="header">
      <div className="logo-container">
        <img className="logo" src="/assets/logo.jpg" />
      </div>
      <div className="nav-items">
        <ul>
          <li>Home</li>
          <li>About us</li>
          <li>Contact us</li>
          <li>Cart</li>
        </ul>
      </div>
    </div>
  );
};

const AppLayout = () => {
  return (
    <div className="App">
      <Header />
    </div>
  );
};

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<AppLayout />);

It is what i am getting

请帮助我,因为我是新反应。

我期待在服务器上显示图像

javascript reactjs image jsx
1个回答
0
投票

如果仍然不起作用,请尝试在上面的代码中添加 alt 标签 试试这个:

从“react”导入React; 从“react-dom/client”导入 ReactDOM;

const Header = () => {
  return (
    <div className="header">
      <div className="logo-container">
        <img className="logo" src={process.env.PUBLIC_URL + "/assets/logo.jpg"} alt="Logo" />
      </div>
      <div className="nav-items">
        <ul>
          <li>Home</li>
          <li>About us</li>
          <li>Contact us</li>
          <li>Cart</li>
        </ul>
      </div>
    </div>
  );
};

const AppLayout = () => {
  return (
    <div className="App">
      <Header />
    </div>
  );
};

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<AppLayout />);
© www.soinside.com 2019 - 2024. All rights reserved.