【问题标题】:In Gatsby.js, use an image as li bullet在 Gatsby.js 中,使用图像作为 li bullet
【发布时间】:2022-02-10 21:29:26
【问题描述】:

我正在尝试将图像用作“li”要点。我正在尝试将图像作为“StaticImage”导入以在样式组件中使用它,如下所示:

import { StaticImage } from "gatsby-plugin-image";

export function Bullet() {
    return <StaticImage src="../images/bullet.png" alt="li bullet" />;
}

然后我尝试了几种不同的解决方案,将其添加为要点,例如,作为伪元素:

        
li {
    &:before {
        content: ${Bullet}
    }
        
}

但我总是得到 [Object object]。有没有办法做到这一点?

【问题讨论】:

    标签: reactjs gatsby gatsby-image


    【解决方案1】:

    为什么不直接使用:

    li {
        &:before {
            content: '../images/bullet.png'
        }
            
    }
    

    甚至:

    import bullet from '../images/bullet.png'
    li {
        &:before {
            content: ${bullet}
        }
            
    }
    

    您可能需要这样做:content: ${props =&gt; props.bullet},具体取决于您的实施

    如果您想使用自己的组件,可以尝试使用props,按照最后一种方法:

    li {
         &:before {
            content: ${props => props.Bullet}
         }   
    }           
    

    【讨论】:

    • 这在 React 中有效,但你确定它在 Gatbsy 中有效吗?它不适合我。即使存在并且控制台错误上的路径正确,它也找不到图像(404)。
    • 如果在 React 中工作在 Gatsby 中工作。尝试修改路径的相对性或从根开始:./path/to/image
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2021-02-22
    相关资源
    最近更新 更多