css - 在 React Typescript 中为按钮组件添加图像背景
问题描述
我正在使用 React 打字稿的 React 项目中使用材质 UI 库。我希望这个按钮有一张图片作为背景。但是,该按钮不接受 src 或 imageURL 并返回打字稿错误,并且 css 样式背景也不显示图片。这是我的代码:
const noticon = require ('./../../images/nicon.png')
const Avatarpic = require ('./../../images/Avatar.png')
const ExampleButton = ({
// useOpenState hook handlers
handleToggle, handleClose, handleOpen, setOpenState, isOpen
}: DropdownButtonProps) => (
<Button onClick={handleToggle} style={{backgroundImage: '{noticon}'}} square>
</Button>
)
function Navbar () {
return (
<>
<TopBar style={{ backgroundColor: '#e6edec' }}>
<TopBarSection>
<TopBarTitle>
<Avatar imgUrl={Avatarpic} name='حسین ساداتی پور' style={{ fontFamily: 'IranSans', fontSize: '20px' }} />
</TopBarTitle>
<Dropdown ButtonComponent={ExampleButton}>
<DropdownItem>Item to click</DropdownItem>
</Dropdown>
</TopBarSection>
{// <TopBarSection>
// burger menu Icon
// </TopBarSection>
}
</TopBar>
<section
style={{
padding: 50,
textAlign: 'center'
}}
>
Some content
</section>
</>
)
}
export default Navbar
解决方案
你需要这样做:
<Button onClick={handleToggle} style={{backgroundImage: `url(${noticon})`}} square>
推荐阅读
- javascript - 使用 jQuery 和 AJAX 删除记录删除不起作用
- javascript - 如何在 HTML 输入文本字段中输入换行符?
- java - Cucumber - WebDriverWait 空指针异常 - Java
- selenium-webdriver - 如何通过selenium java找到一个元素在序列中的位置
- mysql - 计算来自具有相同架构的不同数据库的查询
- sparql - 说明 MIN/MAX 聚合器如何处理未绑定的可选值
- javascript - Postgres“for Each” RangeError:超出最大调用堆栈大小
- bash - 在 Windows bash 中查找字符串的一部分
- python - 已解决:C 和 Python:通过 mmap 填充文件为空
- sql - 数据库查询 - 异常查询