找不到模块:无法解析'material-ui/styles/colors'

zer*_*ing 9 javascript reactjs material-ui

我有以下代码,没有编译:

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;
Run Code Online (Sandbox Code Playgroud)

它说:

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'  
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Chi*_*dra 9

在这里的评论中移动讨论:

确保安装next材料版本-ui:

npm install material-ui@next

此import语句不正确:

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

  },
});
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,键primary secondaryerror接受颜色对象或'调色板意图',这是一个如下所示的对象:

interface PaletteIntention {
  light?: string;
  main: string;
  dark?: string;
  contrastText?: string;
};
Run Code Online (Sandbox Code Playgroud)

唯一需要的密钥是main.其余部分是根据main未提供的值自动计算的.

参考:

文档有一个关于主题的详细部分,详细解释了所有这些.

  • 我提名您重写他们的官方文档。 (4认同)