我正在使用material-ui的1.2.1版本,我试图覆盖AppBar组件是透明的。该文档概述了如何覆盖样式here。
我的代码:
import React, { Component } from 'react';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import logo from '../Assets/logo.svg';
class NavigationBar extends Component {
render() {
const styles = {
root: {
backgroundColor: 'transparent',
boxShadow: 'none',
},
};
return (
<AppBar position={this.props.position} classes={{ root: styles.root }}>
<Toolbar>
<img src={logo} style={{ height: '28px' }} />
</Toolbar>
</AppBar>
);
}
}
export default NavigationBar;
但这没有结果。我试图覆盖错误吗?不知道我在哪里错了...
这个答案让@Nadun的答案完整 - 他值得信任。要覆盖材料ui类,我们需要了解这些内容
1.在顶部的const变量中添加样式
const styles = {
root: {
backgroundColor: 'transparent !important',
boxShadow: 'none',
paddingTop: '25px',
color: '#FFFFFF'
}
};
2.我们需要使用带有“withStyles”的高阶函数来覆盖材料ui类
export default withStyles(styles)(NavigationBar);
3.现在我们可以使用这些样式作为渲染函数中的道具尝试这样做 - console.log(this.props.classes)
- 你得到一个类名称对应你在样式对象中包含的属性。喜欢 - {root: "Instructions-root-101"}
。
使用classes属性添加它
render() {
const { classes } = this.props;
return (
<AppBar classes={{ root: classes.root }}>
// Add other code here
</AppBar>
)
}
import React, { Component } from 'react';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import logo from '../Assets/logo.svg';
import { withStyles } from '@material-ui/core/styles';
const styles = {
transparentBar: {
backgroundColor: 'transparent !important',
boxShadow: 'none',
paddingTop: '25px',
color: '#FFFFFF'
}
};
class NavigationBar extends Component {
render() {
return (
<AppBar className={classes.transparentBar}>
<Toolbar>
<img src={logo} style={{ height: '28px' }} />
</Toolbar>
</AppBar>
);
}
}
export default withStyles(styles)(NavigationBar);
找到重要的部分:
backgroundColor: 'transparent !important'
有关更多详细信息,请参阅本指南:https://material-ui.com/customization/overrides/
希望对你有帮助