javascript - 如何设置标题 zIndex?出现以下错误:无法读取未定义的属性“zIndex”
问题描述
我试图通过更改它的 z-index 将我的抽屉组件保留在标题中,但由于我收到错误消息而无法这样做:无法读取未定义的属性“zIndex”
谁能让我知道我哪里出错了?
import React, { Component } from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import AppBar from "@material-ui/core/AppBar";
import Button from "@material-ui/core/Button";
import IconButton from "@material-ui/core/IconButton";
import MenuIcon from "@material-ui/icons/Menu";
import Drawer from '@material-ui/core/Drawer';
import CssBaseline from '@material-ui/core/CssBaseline';
import Toolbar from '@material-ui/core/Toolbar';
import List from '@material-ui/core/List';
import Typography from '@material-ui/core/Typography';
import Divider from '@material-ui/core/Divider';
import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
import InboxIcon from '@material-ui/icons/MoveToInbox';
import MailIcon from '@material-ui/icons/Mail';
const drawerWidth = 240;
const styles = theme => ({
root: {
display: "flex",
justifyContent: "space-between"
},
appBar: {
zIndex:theme.zIndex.drawer + 1,
},
drawer: {
width: drawerWidth,
flexShrink: 0,
},
drawerPaper: {
width: drawerWidth,
},
toolbar: theme.mixins.toolbar,
});
class Header extends Component {
render() {
return (
<div>
<AppBar position="static" style={styles.appBar}>
<Toolbar style={styles.root}>
<Typography color="inherit"> NEWS</Typography>
<Button color="inherit">LOGIN</Button>
</Toolbar>
<Drawer
style={styles.drawer}
variant="permanent"
style={{
paper: styles.drawerPaper,
}}
>
<div style={styles.toolbar} />
<List>
{['Inbox', 'Starred', 'Send email', 'Drafts'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
<Divider />
<List>
{['All mail', 'Trash', 'Spam'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
</Drawer>
</AppBar>
</div>
);
}
}
export default Header;
我正在尝试将 appbar 的 zindex 设置为大于抽屉 z-index 但它不起作用
解决方案
您的代码的问题是您无法访问样式对象,直到它完全实例化。您可能想要做的是创建styles
一个将主题作为参数并返回样式对象的函数,如下所示:
const styles = theme=>({
root: {
display: "flex",
justifyContent: "space-between"
},
appBar: {
zIndex:theme.zIndex.drawer + 1,
},
drawer: {
width: drawerWidth,
flexShrink: 0,
},
drawerPaper: {
width: drawerWidth,
},
toolbar: theme.mixins.toolbar,
});
Header
然后通过使用withStyles
组件包装器包装它并将您的样式传递给它来导出您的组件。
export default withStyles(styles)(Header);
这是完整的代码:
import React, { Component } from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import AppBar from "@material-ui/core/AppBar";
import Button from "@material-ui/core/Button";
import IconButton from "@material-ui/core/IconButton";
import MenuIcon from "@material-ui/icons/Menu";
import Drawer from '@material-ui/core/Drawer';
import CssBaseline from '@material-ui/core/CssBaseline';
import Toolbar from '@material-ui/core/Toolbar';
import List from '@material-ui/core/List';
import Typography from '@material-ui/core/Typography';
import Divider from '@material-ui/core/Divider';
import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
import InboxIcon from '@material-ui/icons/MoveToInbox';
import MailIcon from '@material-ui/icons/Mail';
const drawerWidth = 240;
const styles = theme => ({
root: {
display: "flex",
justifyContent: "space-between"
},
appBar: {
zIndex:theme.zIndex.drawer + 1,
},
drawer: {
width: drawerWidth,
flexShrink: 0,
},
drawerPaper: {
width: drawerWidth,
},
toolbar: theme.mixins.toolbar,
});
class Header extends Component {
render() {
const {classes} = this.props;
return (
<div>
<AppBar position="static" className={classes.appBar}>
<Toolbar className={classes.root}>
<Typography color="inherit"> NEWS</Typography>
<Button color="inherit">LOGIN</Button>
</Toolbar>
<Drawer
className={classes.drawer}
variant="permanent"
style={{
paper: styles.drawerPaper,
}}
>
<div className={classes.toolbar} />
<List>
{['Inbox', 'Starred', 'Send email', 'Drafts'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
<Divider />
<List>
{['All mail', 'Trash', 'Spam'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
</Drawer>
</AppBar>
</div>
);
}
}
export default withStyles(styles)(Header);
请注意,使用withStyles
包装器传递给组件的这些样式是通过classes
props 访问的。然后,您可以使用该className
属性为组件设置样式。
推荐阅读
- javascript - 如何在套件脚本 2.0 中将 xml 转换为 JSON
- tensorflow - 了解高维密集层的输出
- c# - (С#, Websockets, Unity) 如何使用 System.Net.Websockets 读取 JSON 数组
- python - python-pptx中是否可以使用系列行?
- javascript - Plotly.js 中带有(计算的)平均线的堆积条形图
- ffmpeg - 如何使用 FFMPEG(延迟不是问题)在不缓冲的 Azure 媒体播放器中获得连续直播?
- java - 包装递归函数总是一个好习惯吗?
- c# - 为什么空白出现在我的 C# TextWriter 文件的末尾?
- php - Laravel artisan 命令 php artisan make:Import is not defined
- leaflet - 如何使相邻的多边形与传单中编辑的多边形对齐?