我有以下代码,没有编译:
import React from 'react';
import { AppBar, Toolbar } from 'material-ui';
import { Typography } from 'material-ui';
import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles';
import {cyan, red} from 'material-ui/colors';
import { red400 } from 'material-ui/styles/colors';
const theme = createMuiTheme({
palette: {
primary: red400,
secondary: cyan,
},
});
const View = (props) => (
<MuiThemeProvider theme={theme}>
<AppBar position="static">
<Toolbar>
<Typography variant="title">
{props.title}
</Typography>
</Toolbar>
</AppBar>
</MuiThemeProvider>
);
export default View;
它说:
Failed to compile
./src/Dashboard/AppBar/Views/View.js
Module not found: Can't resolve 'material-ui/styles/colors' in '/home/developer/Desktop/react-reason/example/src/Dashboard/AppBar/Views'
我做错了什么?
评论中的讨论移至此处:
确保安装next
版本的 material-ui:
npm install material-ui@next
此导入语句不正确:
import { red400 } from 'material-ui/styles/colors'
它需要像:
import red from 'material-ui/colors/red';
在这里,red
就是文档所说的"颜色对象"。然后,您可以使用它来创建主题对象,如下所示:
const theme = createMuiTheme({
palette: {
primary: {
main: red[500], //OR red['A400'] for the accent range
contrastText: '#fff', // The text color to use
// You can also specify light, dark variants to use (it's autocomputed otherwise)
}
//You can also just assign the color object if the defaults work for you
secondary: red,
error: red
//tonalOffset
//contrastThreshold
},
});
在上面的代码中,键primary
secondary
和error
接受颜色对象或"调色板意图",这是一个看起来像这样的对象:
interface PaletteIntention {
light?: string;
main: string;
dark?: string;
contrastText?: string;
};
唯一需要的密钥是 main
。其余的将根据 main
的值自动计算(如果未提供(。
参考:
文档有一个关于主题的详细部分,详细解释了所有这些。
试试这个
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
就我而言,我下载了一个模板材料仪表板专业版并正在运行 npm install
安装依赖项,但它显示相同的错误,所以我用
npm install material-ui@next --force
通过添加--force
标志解决了我的问题。 发布此内容是因为可能对某人有所帮助。