简体   繁体   English

在 App.js 文件中作为道具传递时,图像未在 DOM 中呈现?

[英]Image is not rendering in the DOM while passing as props in the App.js file?

I am passing the image name as props in the instance of the Card component in App.js.我在 App.js 中的 Card 组件实例中将图像名称作为道具传递。 Then I am receiving it from the Card component and render it to the DOM.然后我从 Card 组件接收它并将其呈现给 DOM。 But my image is not showing in the DOM.但是我的图像没有显示在 DOM 中。 What could be the reason?可能是什么原因?

This is my App.js这是我的 App.js

import React from "react";
import "./style.css";
import Navbar from "./components/Navbar";
import Hero from "./components/Hero";
import Card from "./components/Card";
import "./images/image12.png";
import "./images/Star1.png";
function App() {
  return (
    <div className="App">
      <Navbar />
      <Hero />
      <Card
        img="image12.png"
        star="Star1.png"
        obtainedRating={5.0}
        TotalRating="6-USA"
        briefText="Life lessons with Katie Zaferes"
        price="$136"
        person="/person"
      />
    </div>
  );
}

export default App;

Here is my Card component这是我的卡片组件

import React from "react";
import "../style.css";
import "../images/image12.png";
import "../images/Star1.png";
export default function Card(props) {
  console.log(props);
  return (
    <div className="card--section">
      <img src={`../images/${props.img}`} className="card--image" alt="Katie" />

      <div className="rating">
        <img
          src={`../images/${props.star}`}
          className="star--image"
          alt="star"
        />
        <small>
          {props.obtainedRating} <span>{props.totalRating}</span>
        </small>
      </div>
      <p className="brief-text">{props.briefText}</p>
      <p className="price">
        <strong>{props.price}</strong>
        {props.person}
      </p>
    </div>
  );
}

This is my Folder structure这是我的文件夹结构

src -components -Card.js -images -image12.png -Star1.png -App.js -index.js src -components -Card.js -images -image12.png -Star1.png -App.js -index.js

This is not working because you need to import an image like this and then put imageName as the value of src attribute .这不起作用,因为您需要导入这样的图像,然后将 imageName 作为 src 属性的值 You can give any name you like.你可以给任何你喜欢的名字。

import imageName from "../images/image12.png";

src={imageName}

Import images like:导入图像,例如:

import img12 from '../images/image12.png'
import star1 from './images/Star1.png'

The image path can retrieved by using the import statement可以使用 import 语句检索图像路径

like below像下面

import "./styles.css";
import clouds from "./Images/clouds.jpeg";
import CardImage from "./CardImage";

export default function App() {
  console.log("cloud path", clouds);
  return (
    <div className="App">
      <CardImage path={clouds} alt="clouds" />
    </div>
  );
}

在此处输入图像描述

I have found the solution.I simply imported我找到了解决方案。我只是导入了

import cardImage from "./images/image12.png"
import star from "./images/Star1.png"

And used them in App.js并在 App.js 中使用它们

<Card img={cardImage} star={star}/>

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

相关问题 Nextjs - 将 props 从自定义 _app.js 传递到<navbar>零件?</navbar> - Nextjs - passing props from custom _app.js to <Navbar> component? Google-Maps-React:将道具传递给 App.js 的问题 - Google-Maps-React: Problems passing props to App.js React Router Dom 组件在 App.js 中呈现,但不是作为单独的组件 - React Router Dom components rendering in App.js, but not as separate component 我正在将一个道具从 App.js 传递给 MovieCard.js,但传递的道具显示为空 - I am passing a prop from App.js to MovieCard.js but props passed is showing empty 将变量从 app.js 传递到 ejs 文件到脚本标签中 - Passing variable from app.js to ejs file into a script tag handleAddOption (app.js:64) 处未定义的道具 - props of undefined at handleAddOption (app.js:64) NextJS - 将客户端道具从 app.js 传递到特定组件 - NextJS - Passing client-side props from app.js to specific components 在同一个app.js中呈现来自不同HTML页面的多个DOM元素 - Rendering multiple DOM elements from different HTML pages in the same app.js React Native:如何将 props 变量从其他 js 文件传递到 app.js 文件 - React Native: How to pass a props variable from other js file to app.js file React 功能组件未在 App.js 中呈现 - React functional components not rendering in App.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM