【问题标题】:React doesn't render a SVG [duplicate]React 不渲染 SVG [重复]
【发布时间】:2021-09-11 18:52:10
【问题描述】:

您好,我在 React 中创建了一个模板,它使用一个对象来填充它。但是 SVG 没有渲染,我不知道为什么。文件夹和de svg图片的路径都是正确的。

这是对象:

export const homeObjOne = {
    id: 'about',
    lightBg: false,
    lightText: true,
    lightTextDesc: true,
    topLine: 'Gym Application',
    headLine: 'For Our Fitness Lovers',
    description: 'Lorem',
    buttonLabel: 'Get started',
    imgStart: false,
    img: require('../../images/svg-1.svg'),
    alt: 'Growth',
    dark: true,
    primary: true,
    darkText: false
};

这是传递de对象的模板:

const InfoSection = ({
  lightBg,
  id,
  imgStart,
  topLine,
  lightText,
  headline,
  darkText,
  description,
  buttonLabel,
  img,
  alt,
  primary,
  dark,
  dark2
}) => {
  return (
    <Img src={img} alt={alt} />
 );
};

export default InfoSection;

我只写了基本代码,因为它更容易阅读和回答问题。该应用程序没有发送任何有关 htat 路径或 de svg-1.svg 不存在的错误,并且该对象的所有其他组件都渲染得很好,所以我不知道出了什么问题。

如果有人可以帮助我,请告诉我。谢谢!

【问题讨论】:

标签: javascript reactjs svg


【解决方案1】:
import imgUrl from '../../images/svg-1.svg';

export const homeObjOne = {
    id: 'about',
    lightBg: false,
    lightText: true,
    lightTextDesc: true,
    topLine: 'Gym Application',
    headLine: 'For Our Fitness Lovers',
    description: 'Lorem',
    buttonLabel: 'Get started',
    imgStart: false,
    img: imgUrl,
    alt: 'Growth',
    dark: true,
    primary: true,
    darkText: false
};

这应该可行。这里可以查看原因:https://create-react-app.dev/docs/adding-images-fonts-and-files/

【讨论】:

  • 是的,它有效,非常感谢!
  • 您是否必须在组件外部导入 svg 并将其/它们全部放在包中?我想从 s3 存储桶中获取 svg,但在代码运行之前我不会有 url/我不想导入所有 svg - 所以需要在组件中导入 - 有什么想法吗?
猜你喜欢
  • 2018-05-23
  • 2021-10-29
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 2017-11-16
  • 2018-11-19
  • 2020-09-18
  • 2019-08-20
相关资源
最近更新 更多