简体   繁体   English

在 Javascript object 中存储图像的最佳方式是什么?

[英]What is the best way to store an image in Javascript object?

I am trying to figure out the best way to store an image in a Javascript object or if that is even possible.我正在尝试找出将图像存储在 Javascript object 或什至可能的情况下的最佳方法。 I am currently building a ReactJS project that will have multiple cards containing information about multiple different projects.我目前正在构建一个 ReactJS 项目,该项目将包含多张卡片,其中包含有关多个不同项目的信息。 I am creating this array of multiple objects and then plan to map the array to create multiple cards.我正在创建这个包含多个对象的数组,然后计划将 map 数组创建为多个卡片。 The only part I am unsure of is how to handle the images that will be the header for each card.我唯一不确定的部分是如何处理每张卡的 header 图像。 Can you store them in an object or do you have to import them in your component and then pass them to the card?您可以将它们存储在 object 中,还是必须将它们导入您的组件然后将它们传递给卡?

export const PROJECTS =
[
    project1 =
    {
        title: "Project 1 Title",
        description: "Project 1 Description",
        linkToProject: "https://linkToProject1.com/",
        image: "./project1ImagePath.png" or can I somehow import the image and then store it here?
    }
]

Example Parent Component示例父组件

return (
    <div>
        { PROJECTS.map((project, index) => {
           return (
              <Card project={project} index={index} />
           })
        }

Example Card Component示例卡片组件

const Card = props => {
     return (
         <div>
            <img src={props.project.image} />
         </div>
}
import Image1 from "./project1ImagePath.png"
import Image2 from "./project1ImagePath.png"

export const PROJECTS =
[

    {
        title: "Project 1 Title",
        description: "Project 1 Description",
        linkToProject: "https://linkToProject1.com/",
        url: Image1
    },

    {
        title: "Project 2 Title",
        description: "Project 2 Description",
        linkToProject: "https://linkToProject2.com/",
        url: Image2
    }
]
return (
    <React.Fragment>
       {
       PROJECTS.map((project, i)=>{
          return <Card project={project} key={i} />
        })
       }
    </React.Fragment>        
 )
const Card = props => {
     return (
         <div>
            <img src={props.project.url} />
         </div>
)}

Yes you can store it in your object, but you also need to import it是的,您可以将其存储在您的 object 中,但您还需要导入它

import Image from "./project1ImagePath.png"

export const PROJECTS =
[
    project1 =
    {
        title: "Project 1 Title",
        description: "Project 1 Description",
        linkToProject: "https://linkToProject1.com/",
        image: Image
    }
]

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

相关问题 用JavaScript存储以下内容的最佳方法是什么? - What is the best way to store the following in Javascript? 将动态网页存储为对象的最佳方法是什么? - What is the best way to store a dynamic webpage as an object? 在JavaScript中删除对象的最佳方法是什么? - What is the best way to delete an object in JavaScript? 在Javascript中检查对象是否为数组的最佳方法是什么? - What is the best way to check if an object is an array or not in Javascript? 用JavaScript编写对象方法的最佳方法是什么? - What is the best way to write object methods in JavaScript? 在javascript中访问位置对象的最佳方法是什么? - What is the best way to access the location object in javascript? 投影 JavaScript 对象字段的最佳方法是什么? - What is the best way to projecting fields of JavaScript object? 在JavaScript中存储非常大的二进制数的最佳方法是什么? - What is the best way to store very large binary numbers in JavaScript? 使用JavaScript或Jquery在浏览器中存储本地值的最佳方法是什么? - what is the best way to store local values in browser using Javascript or Jquery? 在JavaScript中存储/读取大型矩阵的最佳方法是什么? - What is the best way to store/read a large matrix in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM