我在try 从我的回购中渲染图片时遇到了问题. 我将组件的框架放在一个库中,并在不同的repo中呈现该组件,但我找不到在UI中显示图片的正确路径.

Here is my code for the component in the library:

<ThemeProvider theme={buyingTheme}>
      <Box display='flex' width='100%' justifyContent='center' alignItems='center'>
        <Box display='flex' sx={{ height:'445px', width:'445px', border: '1px solid red' }}>
          <Box
            height='306px'
            width='333px'
            sx={{
              paddingTop: '70px',
              paddingLeft: '47px',
              backgroundImage: `url(${picture})`,
              backgroundPosition: 'center',
              backgroundRepeat: 'no-repeat',
              backgroundSize: 'contain',
            }}
          >
            {/* <img src={emptyStateLight} alt="Empty State" /> */}
          </Box>
        </Box>
        <Box display='flex' flexDirection='column' alignItems='left' sx={{ height:'136px', width:'445px', border: '1px solid blue', marginLeft: '20px' }}>
          <Typography variant="headings.h3" sx={{ height:'24px', paddingTop:'4px', paddingBottom:'4px' }}>{title}</Typography>
          <Typography
            variant="body.default"
            color="text.secondary"
            sx={{
              whiteSpace: 'pre-line',
              wordWrap: 'break-word',
              marginTop: '4px', height:'48px',
              paddingBottom:'4px', marginBottom: '4px',
            }}
          >
            {description}
          </Typography>
          <Button
            sx={{ height: '40px', width: '93px', padding:'20px', marginTop: 'auto' }}
            onClick={handleOnClick}
          >
            {buttonText}
          </Button>
        </Box>
      </Box>
    </ThemeProvider>

Here is the code in the repo

<EmptyState
        picture="public/picture.png"
        title="Title"
        description="Description multi-line Description
          multi-line Description
          multi-line"
        handleOnClick={() => {
          // TODO: Define your click handler function here
        }}
        buttonText="Button"
      />

图片位于PUBLIC--&gt;PUBLIC\Picture.png文件夹中,我的组件位于--&gt;src\pages\Layout\CollectionsTable\CollectionsTable.tsx中

我如何修复问题并显示图片? 我try 了许多方法,但都没有成功.

推荐答案

在典型的Reaction应用程序中,当您为背景图像指定100时,它应该是基于公共目录的图像文件的相对路径,公共目录是静态资源的根.以下是调整代码的方法:

picture.png文件移到您的Reaction应用程序的public目录中(如果它还不在那里).所以,你应该有一个这样的 struct :

public
|-- picture.png
src
|-- pages
    |-- Layout
        |-- CollectionsTable
            |-- CollectionsTable.tsx

CollectionsTable.tsx组件中,将url指定为picture.png文件的相对路径:

<ThemeProvider theme={buyingTheme}>
  <Box display='flex' width='100%' justifyContent='center' alignItems='center'>
    <Box display='flex' sx={{ height:'445px', width:'445px', border: '1px solid red' }}>
      <Box
        height='306px'
        width='333px'
        sx={{
          paddingTop: '70px',
          paddingLeft: '47px',
          backgroundImage: `url(./picture.png)`, // Relative path to the picture
          backgroundPosition: 'center',
          backgroundRepeat: 'no-repeat',
          backgroundSize: 'contain',
        }}
      >
        {/* <img src={emptyStateLight} alt="Empty State" /> */}
      </Box>
    </Box>
    {/* Rest of your component */}
  </Box>
</ThemeProvider>

通过指定./picture.png,您将从公共目录的根目录引用Picture.png文件,该根目录是Reaction应用程序中提供图像等静态资源的位置.

现在,当您在repo中使用EmptyState组件并将"public/picture.png"指定为pictureprops 时,它应该正确地引用图像

<EmptyState
  picture="public/picture.png"
  // Rest of your props
/>

Javascript相关问答推荐

Mongodb拥有5亿个文档,我想根据JavaScript驱动程序中的两个字段使用regEx进行搜索,而不是模式

橡皮擦完全让画布变成白色

积分计算和 colored颜色 判断错误

如何禁用附加图标点击的v—自动完成事件

如何用拉威尔惯性Vue依赖下拉?

使用Promise.All并发解决时,每个promise 的线性时间增加?

如何在Svelte中从一个codec函数中调用error()?

如何在FastAPI中为通过file:/URL加载的本地HTML文件启用CORS?

按下单键和多值

如何利用CSS中的隐藏元素实现平滑扩展和防止网格行间隙

从逗号和破折号分隔的给定字符串中查找所有有效的星期几

通过跳过某些元素的对象进行映射

在Java脚本中录制视频后看不到曲目

如何根据查询结果重新排列日期

用另一个带有类名的div包装元素

如何使用[ModelJSON,ArrayBuffer]调用tf.loadGraphModelSync

未找到用于 Select 器的元素:in( puppeteer 师错误)

ComponentWillReceiveProps仍在React 18.2.0中工作

在将元素追加到DOM之前,createElement()是否会触发回流?混淆abt DocumentFragment行为

在高位图中显示每个y轴系列的多个值