我创建了一个AppBar
组件,我想将该组件导出到App
组件中。
src / components / appbar.jsx:
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import IconButton from '@material-ui/core/IconButton';
import MenuIcon from '@material-ui/icons/Menu';
import LeftDrawer from './leftDrawer';
const styles = {
root: {
flexGrow: 1,
},
flex: {
flexGrow: 1,
},
menuButton: {
marginLeft: -12,
marginRight: 20,
},
};
class ButtonAppBar extends React.Component {
state = {
left: false,
}
toggleDrawer = (side, open) => () => {
this.setState({
[side]: open,
});
};
render() {
const { classes } = this.props;
return (
<div className={classes.root}>
<AppBar position="static">
<Toolbar>
<IconButton
className={classes.menuButton}
color="inherit"
aria-label="Menu"
onClick={this.toggleDrawer('left', true)}
>
<MenuIcon />
</IconButton>
<LeftDrawer
open={this.state.left}
toggleDrawer={this.toggleDrawer}
/>
</Toolbar>
</AppBar>
</div>
);
};
}
ButtonAppBar.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(ButtonAppBar);
src / app.jsx:
import React from 'react';
import ButtonAppBar from './components/appBar';
const App = () => (
<div>
<ButtonAppBar />
</div>
);
export default App;
.estlinrc.json:
{
"extends": [
"airbnb", "prettier"
],
"env": {
"browser": true
},
"rules": {
"react/forbid-prop-types": "off"
},
"settings": {
"import/extensions": [".jsx", ".js"],
"import/resolver": {
"webpack": {
"config": "webpack.config.js"
}
}
}
}
.babelrc.json:
{
"presets": [
"env",
"react"
],
"plugins": ["transform-class-properties"]
}
package.json:
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"eslint": "^5.4.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-config-prettier": "^3.0.1",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.1",
"eslint-plugin-react": "^7.11.1",
"prettier": "^1.14.2",
"webpack": "^4.17.0",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
}
当我执行指令时:
./node_modules/.bin/eslint --ext .js,.jsx ./
我收到此错误:
Parse errors in imported module './components/appBar': Unexpected token = (24:9) import/no-named-as-default
我在How do I resolve eslint import/no-named-as-default处发现了类似的问题,在接受的答案中,它禁用了规则import/no-named-as-default
。
有什么解决办法可以保留此规则并更新代码以免出现此错误?
答案 0 :(得分:3)
似乎您不是在使用babel-eslint作为解析器,在设置中缺少babel-transform-class-properties插件,或者eslint未配置为使用它,或者它是eslint的较旧版本。
希望其中一个是罪魁祸首!