简体   繁体   English

如何从 JavaScript 文件中的项目文件夹加载图像?

[英]How do I load an image from my project folder inside a JavaScript file?

I am trying to build a website and I currently am using a data.js file to be the source of images and links in one of my components.我正在尝试建立一个网站,目前我正在使用 data.js 文件作为我的一个组件中图像和链接的来源。 The file structure looks like this right now.文件结构现在看起来像这样。

在此处输入图像描述

And my data.js file looks something like this:我的 data.js 文件看起来像这样:

export const products = [
  {
    id: 1,
    img: "",
    link: "http://lama.dev",
  },
  {
    id: 2,
    img: "",
    link: "http://lama.dev",
  },
];

I wish to use the images under my 'img' folder to be inside the img: "xxx" part but I am unsure how.我希望将我的“img”文件夹下的图像用于img: "xxx"部分,但我不确定如何使用。 Is this even possible?这可能吗?

Not sure if this is needed but this is how I intend to use my data.js file:不确定是否需要这样做,但这就是我打算使用我的 data.js 文件的方式:

import "./product.css";

const Product = ({ img, link }) => {
  return (
    <div className="p">
      <div className="p-browser">
        <div className="p-circle"></div>
        <div className="p-circle"></div>
        <div className="p-circle"></div>
      </div>
      <a href={link} target="_blank" rel="noreferrer">
        <img src={img} alt="" className="p-img" />
      </a>
    </div>
  );
};

export default Product;

you can import your image from your img folder like this.您可以像这样从您的 img 文件夹中导入图像。

import FirstImg from "./img/pexels-cátia-matos-1072179.jpg";

and you can use like this in same file你可以在同一个文件中这样使用

export const products = [
  {
    id: 1,
    img: FirstImg,
    link: "http://lama.dev"
  }
];

or或者

You can change your img folder location to public您可以将您的 img 文件夹位置更改为公共

Folder structer image文件夹结构图

public/img公共/img

and you can use like this你可以这样使用

export const products = [
  {
    id: 1,
    img: "./img/470a19a36904fe200610cc1f41eb00d9.jpg",
    link: "http://lama.dev"
  }
];

Try this in your data.js在你的 data.js 中试试这个

import figma from './me.jpg'
export const products = [
  {
    id: 1,
    img: figma,
    link: "http://lama.dev",
  },
  {
    id: 2,
    img: "",
    link: "http://lama.dev",
  },
];

You can move your images folder under public directory.您可以将图像文件夹移动到公共目录下。 Otherwise all those images would be bundled when you create your react app.否则,当您创建 React 应用程序时,所有这些图像都将被捆绑。

You can then make your data.js to然后你可以让你的 data.js 到

const products = [
  {
    id: 1,
    img: '/img/image.jpg',
    link: 'http://file/'
  }

]

export default products

use require method in js在js中使用require方法

const products = [
  {
    id: 1,
    img: require('/img/image.jpg'),
    link: 'http://file/'
  }

]

export default products

暂无
暂无

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

相关问题 如何在嵌套在视图内的部分视图内加载javascript文件? MVC5 - How do I load my javascript file inside my partial view that is nested inside a view? MVC5 每次页面刷新时,如何将JavaScript文件中的随机图像加载到HTML中? - How do I load a random image from an array in a JavaScript file into HTML every time the page refreshes? Ionic:如何从json文件中仅加载1张图片? - Ionic: How do i load only 1 image from my json file? 如何从文件选择器加载图像并将其加载到 canvas 以便我可以使用 Javascript 对其进行编辑? - How do I load the image from file chooser and get it to load on the canvas so that i can edit it using Javascript? 如何从我的eclipse项目中删除javascript验证? - How do I remove javascript validation from my eclipse project? 在 JavaScript 模块中,如何从外部文件加载 class? - In a JavaScript module, how do I load a class from an external file? 如何从外部文件加载Javascript? - How do I load Javascript from an external file? 如何使用jQuery / JavaScript将子文件夹中的所有文件名加载到我的安全网页中 - How to load all the file names from a sub folder into my secure web page, using jQuery/JavaScript 如何打开Javascript对象中我的项目文件夹中的transform xml文件 - how open transform xml file that is in my project folder in Javascript Object 如何使用纯javascript或jQuery将我的HTML5画布屏幕作为png图像保存到我项目的特定文件夹中? - How can I save my HTML5 canvas screen as a png image to a specific folder of my project with only pure javascript or jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM