【问题标题】:module not found in react js when using a path of image使用图像路径时在反应 js 中找不到模块
【发布时间】:2022-09-27 18:25:27
【问题描述】:

我刚开始反应 js,但在将图像路径导入网页时遇到了麻烦。我有一个名为 assets 的文件夹,它有图像,我想在我的项目中使用这些图像,但是当它作为代码中的输入提供时,它显示:\"ERROR in ./src/containers/products/Products.jsx 7: 0-33 未找到模块:错误:无法解析 \'../../assets/\'\"

这是以下代码

import React from \'react\';
import \'./products.css\';
import {Prodfeat} from \'../../components\'
import logo from \'../../assets/\';

const Products = () => {
    return (
      <div className=\'gpt3__products\' id=\'products\'>
        <div className=\'gpt3__products-heading\'>
          <h1>We have something for everyone!</h1>
        </div>
        <div>
          <Prodfeat title=\'product 1\' text=\'description of product1\' productimg={`${logo}blog01.png`}/>
          <Prodfeat title=\'product 2\' text=\'description of product2\' productimg={`${logo}blog02.png`}/>
          <Prodfeat title=\'product 3\' text=\'description of product3\' productimg={`${logo}blog03.png`}/>
        </div>

      </div>
    )
  }

这是 prodfeat 组件的代码:

import React from \'react\';
import \'./prodfeat.css\';

const Prodfeat = ({title, text, productimg}) => {
  return (
    <div className=\'gpt3__prodfeat\' >
        <div className=\'gpt3__prodfeat-desc\'>
            <div className=\'gpt3__prodfeat-heading\'>
                <h2>{title}</h2>
            </div>
            <div className=\'gpt3__prodfeat-text\'>
                <p>{text}</p>
            </div>
        </div>
        <img src={productimg} alt={title}/>
    </div>
  )
}

我想使用 {logo} 作为参考变量并指向照片,这样我就不必一次又一次地编写路径,但它不接受它作为路径,而只是一个字符串。如果您有任何建议,请指导我解决这个问题。 谢谢

  • const logoPath = \'../../assets\'; - 将路径分配给变量而不是导入。
  • 您的图片在 src 文件中吗?
  • 您的图片在 src 文件中吗?
  • 是的,它们在 src 文件中@monim
  • 我也尝试将路径分配给变量,但它被视为字符串@Andy

标签: javascript html css reactjs


【解决方案1】:

使用此导入 import logo from '../../assets/logo.&lt;file_format&gt;';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-09
    • 2021-11-24
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2021-10-12
    相关资源
    最近更新 更多