繁体   English   中英

Img 未加载,但 alt 标签是

[英]Img not loading but alt tag is

我在尝试加载正常工作的图像时遇到了问题。 我尝试了一些解决方案,从安装 webpack 到从源文件导入 img,但都没有运气。 当我在开发人员模式下进入检查器时,它显示我的 alt 已加载,但显示 img 错误。当我在开发人员模式下编译所有代码或控制台中的任何代码时,我没有收到任何错误

Data.js文件位于多个文件夹中,例如 component>InfoSection>Data.js

export const homeObjOne = {
id: 'home',
lightBg: false,
lightText: true,
lightTextDesc: true,
topLine: 'test',
headline: 'tester in the making',
description: 'this will be the best website ever to exist',
buttonLabel: 'GO',
imgStart: true,
img: require('../../images/hacker.svg'),
alt: 'hacker',
dark: true,
primary: true,
darkText: false
}

index.js也在与Data.js文件相同的文件夹中

import React from 'react'
import {Button} from '../ButtonElements';
import {
InfoContainer,
InfoWrapper,
InfoRow,
Column1,
Column2,
TextWrapper,
TopLine,
Heading,
Subtitle,
BtnWrap,
ImgWrap,
Img
} from './InfoElements';

const InfoSection = ({lightBg, id, imgStart, topLine,lightText,
                headline,darkText,description,buttonLabel,img,alt}) => {
return ( 
 <>
  <InfoContainer lightBg={lightBg} id={id}>
    <InfoWrapper>
      <InfoRow imgStart={imgStart}>
        <Column1>
          <TextWrapper>
            <TopLine>{topLine}</TopLine>
            <Heading lightText={lightText}>{headline}</Heading>
            <Subtitle darkText={darkText}>{description}</Subtitle>
            <BtnWrap>
              <Button to='Home'>{buttonLabel}</Button>
            </BtnWrap>
          </TextWrapper>
        </Column1>
        <Column2>
          <ImgWrap>
            <Img src={img} alt={alt}/>
          </ImgWrap>
        </Column2>
      </InfoRow>
     </InfoWrapper>
   </InfoContainer>  
  </>
  )
 }

export default InfoSection

InfoElements.js也在与上述两个文件相同的文件夹中

import styled from 'styled-components';

export const InfoContainer = styled.div`
color: #fff;
background: ${({lightBg}) => (lightBg ? '#f9f9f9' : '#010606')};

@media screen and (max-width: 768px){
padding: 100px 0;
}
`;

export const InfoWrapper = styled.div`
display: grid;
z-index: 1;
height: 860px;
width: 100%;
max-width: 1100px;
margin-right: auto;
margin-left: auto;
padding: 0 24px;
justify-content: cneter;
`;

export const InfoRow = styled.div`
display: grid;
grid-auto-columns: minmax(auto, 1fr);
align-items: center;
grid-template-areas: ${({imgStart}) => (imgStart ? `'col2 col1'` : `'col1 col2'`)};

@media screen and (max-width: 768px){
grid-template-areas: ${({imgStart}) => (imgStart ? `'col1' 'col2'` : `'col1 col1' 'col2 col2'` )} 
}
`;

export const Column1 = styled.div`
margin-bottom: 15px;
padding: 0 15px;
grid-area: col1;
`;

export const Column2 = styled.div`
margin-bottom: 15px;
padding: 0 15px;
grid-area: col2;
`;

export const TextWrapper = styled.div`
max-width: 540px;
padding-top: 0;
padding-bottom: 60px;
`;

export const TopLine = styled.p`
color: #01bf71;
font-size: 16px;
line-height: 16px;
font-weight: 700;
letter-spacing: 1.4px;
text-transform: uppercase:
margin-bottom:  16px;
`;

export const Heading = styled.h1`
margin-bottom: 24px;
font-size: 48px;
line-height: 1.1;
font-weight: 600;
color: ${({lightText}) => (lightText ? '#f7f8fa' : '#010606')};

@media screen and (max-width: 480px){
font-size: 32px;
}
`;

export const Subtitle = styled.p`
max-width: 440px;
margin-bottom:  35px;
font-size: 18px;
line-height: 24px;
color: ${({darkText}) => (darkText ? '#010606' : '#fff')};
`;

export const BtnWrap = styled.div`
display: flex;
justify-content: flex-start;
`;

export const ImgWrap = styled.div`
max-width: 555px;
height: 100%;
`;

export const Img = styled.img`
width: 100%;
margin: 0 0 10px 0;
padding-right: 0;
`;

它看起来像这样渲染,这就是为什么看不到图像。

<img src="[object Object]" alt="fdsfgds">

您必须更改访问图像的方式。

import img from '../../images/hacker.svg';

 <img src={img} alt="fdsfgds">

因此,不要使用包含 img 的 Data.js 文件,然后将其调用到 index.js 文件中的 Img 片段。 我在 index.js 文件中导入了路径,并将其调用到 Img 片段

暂无
暂无

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

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