Javascript 在jss中为backgroundImage动态插入url时,我没有得到任何图像。反应,材料界面
我可以通过导入图像并在useStyles中使用变量来静态显示图像,如下所示:Javascript 在jss中为backgroundImage动态插入url时,我没有得到任何图像。反应,材料界面,javascript,reactjs,material-ui,jss,Javascript,Reactjs,Material Ui,Jss,我可以通过导入图像并在useStyles中使用变量来静态显示图像,如下所示: import React from 'react' import { makeStyles } from '@material-ui/core/styles' import { Box } from '@material-ui/core' import Image from '../../Images/Shanghai.png' const useStyles = makeStyles(img => ({
import React from 'react'
import { makeStyles } from '@material-ui/core/styles'
import { Box } from '@material-ui/core'
import Image from '../../Images/Shanghai.png'
const useStyles = makeStyles(img => ({
image: {
backgroundImage: 'url('+ image +')',
backgroundPosition: 'center',
width: '100%',
height: '10rem',
backgroundSize: 'cover'
}
}))
const ImageHeader = props => {
const img = props.img
const classes = useStyles(img)
return <Box sm={12} className={classes.image} />
}
export default ImageHeader
没有显示图像,我得到以下值:
backgroundImage: url([object object])
感谢您的帮助。根据:
使用样式。它接受一个参数:将要使用的属性
用于样式表中的“插值”
基本上,makeStyles
回调中的参数表示作为对象的主题。为了在makeStyles
的回调中使用传递给useStyles
的参数,必须对backgroundImage
属性使用回调方法
。。。
const useStyles=makeStyles(主题=>({//'url('+image+')),//{
常量img=props.img
常量类=使用样式(img)
...
url()正在等待类似字符串的格式。它似乎接收到一个对象,是否尝试console.log(img)查看该对象的外观?是,如果我console.log(img)我得到了url的字符串值。据我所知,它应该可以工作,但它不能。你确定它是一个字符串,它不应该显示为object在makeStyles
中传递给函数的参数就是主题。你只是碰巧调用了主题img
。感谢gdh,我按照你的建议,回到了MUI文档,pro问题解决了!
backgroundImage: url([object object])