简体   繁体   English

React 无法加载我的本地图像,如果我使用 require function,则会出现错误

[英]React can't load my local image and if I use require function, there is an error

I made two files and my first file imported second file.我制作了两个文件,第一个文件导入了第二个文件。 But it cannot load my local images.但它无法加载我的本地图像。 It shows image like this它显示这样的图像

enter image description here在此处输入图像描述

and to solve this problem I tried to use require function but when I used it, there is an error为了解决这个问题,我尝试使用 require function 但是当我使用它时,出现错误

[Parsing error: Unexpected token, expected ","] [解析错误:意外的标记,应为“,”]

this is NaviIconList.jsx这是 NaviIconList.jsx

import React from "react";
import NaviIcon from "./NaviIcon";
import "../navi.css";
import cart from "./images/shopping-cart.png"
import bell from "./images/user.png"
import user from "./images/user.png"

const image = [
  {
   image : "./images/shopping-cart.png"
  },
  {
    image : {bell}
  },
  {
    image : {user}
  },
];

function NaviIconList (props) {
  return (
    <div id="NaviIconList">
      {image.map((image) => {
        return (
          <NaviIcon image={image.image} />
        );
      })}
    </div>
  );
}

export default NaviIconList;

and this is NaviIcon.jsx这是 NaviIcon.jsx

import React from "react";
import "../navi.css"

function NaviIcon (props) {
  return (
    <div id="NaviIcon">
      <img src={props.image} />
    </div>
  );
}

export default NaviIcon;

finally, this is NaviIcon.jsx with require function最后,这是 NaviIcon.jsx 需要 function

import React from "react";
import "../navi.css"

function NaviIcon (props) {
  return (
    <div id="NaviIcon">
      <img src={require({props.image})}/>
    </div>
  );
}

export default NaviIcon;

You don't need to import your images, just build an array of image paths and use it:您不需要导入图像,只需构建一个图像路径数组并使用它:

 const imgs = [ "./images/shopping-cart.png", "./images/user.png", "./images/user.png" ]

then rewrite this part:然后重写这部分:

 function NaviIconList (props) { return ( <div id="NaviIconList"> {image.map((image) => { return ( <NaviIcon image={image.image}/> ); })} </div> ); } // to: function NaviIconList (props) { return ( <div id="NaviIconList"> {image.map((image) => { return ( <NaviIcon image={image} /> ); })} </div> ); }

finally your NavIcon component will look:最后,您的 NavIcon 组件将如下所示:

 function NaviIcon (props) { return ( <div id="NaviIcon"> <img src={props.image}/> </div> ); }

If you remove {} from your variable name, it will be fixed.如果您从变量名称中删除 {},它将得到修复。 Also, don't forget to add key prop to make your array item unique.另外,不要忘记添加 key 道具以使您的数组项独一无二。 Your code should be:你的代码应该是:

import React from "react";
import NaviIcon from "./NaviIcon";
import "../navi.css";
import cart from "./images/shopping-cart.png"
import bell from "./images/user.png"
import user from "./images/user.png"

const image = [
  {
    image : cart
  },
  {
    image : bell
  },
  {
    image : user
  },
];

function NaviIconList (props) {
  return (
    <div id="NaviIconList">
      {image.map((image) => {
        return (
          <NaviIcon key={image} image={image.image} />
        );
      })}
    </div>
  );
}

export default NaviIconList;

Don't put {} everwhere不要把{}放在任何地方

const image = [
  {
   image : "./images/shopping-cart.png"
  },
  {
    image : bell
  },
  {
    image : user
  },
];

{user} is the same as { user: user } {user}{ user: user }相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM