【问题标题】:Exporting images and font-awesome icons from seperate js file从单独的 js 文件中导出图像和字体真棒图标
【发布时间】:2020-11-01 21:41:00
【问题描述】:

我已经阅读了很多关于如何将图像存储在对象数组中的解决方案。我做了每一个解决方案,但没有一个对我有用。也许我错过了演员表?

图像存储在src 文件夹中。

我试过export default,可惜没解决问题。

导出部分(MenuList.js):

import burgerImg from '../images/menu-images/burger-menu2.png'
import pizzaImg from '../images/menu-images/pizza-menu1.png'
import kebabImg from '../images/menu-images/kebab-menu1.png'
import {FaHamburger} from 'react-icons/fa'
import {FaPizzaSlice} from 'react-icons/fa'
import {GiMeal} from 'react-icons/gi'


export const MenuList = [
    {
        id: 1,
        name: 'Burgers',
        img: burgerImg,
        icon: FaHamburger
    },
    {
        id: 2,
        name: 'Pizzas',
        img: pizzaImg,
        iconImg: FaPizzaSlice
    },
    {
        id: 3,
        name: 'Kebabs',
        img: kebabImg,
        icon: GiMeal
    }
]

导入部分(Menu.js):

import {MenuList} from './MenuList'

 state = {
    menu: MenuList
}
   {this.state.menu.map(item => {
               return <MenuItem key={item.id} itemInfo={item}/>
   })}

MenuItem.js:

export default function MenuItem({itemInfo}) {
return (
    <div>
        {itemInfo.name}
        {itemInfo.img}
        {itemInfo.icon}
    </div>
)
}

然后我尝试像这样从单个组件加载所有内容:

export default function HomeMenu() {
return (
    <div>
        <h1>Menu</h1>
        <Menu></Menu>
    </div>
)
}

我正在映射列表中的每个元素并将这些元素传递给另一个组件,但问题是我的图像和字体很棒的图标没有加载。

当我运行项目或刷新它时,我得到以下结果:

Burgers/static/media/burger-menu2.d3dca9de.png

控制台输出如下:

index.js:1 Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.

图像的开发工具面板元素部分显示:

文件夹结构:

我错过了关于某事的演员表吗?我查看了很多关于导入/导出图像的堆栈,但没有一个解决方案适合我。

【问题讨论】:

  • 错误是什么? 404的?您如何提供构建文件?因为它是 burger-menu2.d3dca9de.png 它的工作,但看起来你没有东西可以提供来自 dist 的图像
  • 您的开发工具元素面板对于图像的src 显示什么。还有字体,它是如何在元素面板中显示的。 ?你能提供他们的截图吗??
  • @LawrenceCherone 图片是 images/menu-images 文件夹内 src 文件夹的一部分。我的组件是组件文件夹内 src 文件夹的一部分。我已经更新了帖子。
  • @Panther 字体根本没有显示在面板中

标签: javascript reactjs image font-awesome


【解决方案1】:

修改你的MenuItem.js为,

export default function MenuItem({itemInfo}) {
return (
    <div>
        {itemInfo.name}
        <img src={itemInfo.img} alt='' />
        {itemInfo.icon}
    </div>
)
}

要修复图标问题,请将组件作为jsx 在对象中传递。修改为:

export const MenuList = [
    {
        id: 1,
        name: 'Burgers',
        img: burgerImg,
        icon: <FaHamburger /> //jsx
    },
...

jsx 只是带​​有一些语法糖的 JavaScript。

【讨论】:

  • 谢谢。现在正在渲染图像,但字体很棒的图标仍然不可见。
  • 你能显示react-icons的结构吗?它们可能是组件或其他东西,而不是图像文件。为问题添加屏幕截图。
  • react-icons.github.io/react-icons 这就是我实现图标的方式。我只需要通过 npm 安装依赖项并导入我需要的任何图标
  • 开发控制台现在输出此警告。 index.js:1 Warning: Invalid value for prop src on &lt;img&gt; tag. Either remove it from the element, or pass a string or number value to keep it in the DOM.。看起来这不是在 DOM 中渲染图像的正确方法
  • 效果很好。一般jsx是通过props.children传递的。但是可以像您的情况一样使用jsx 作为对象的一部分。
猜你喜欢
  • 2022-11-10
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 1970-01-01
  • 2015-06-01
  • 2013-12-07
  • 2017-05-02
相关资源
最近更新 更多