React教程(十七) : Material UI - 基础

换了台笔记本,重新安装环境,遇到些奇怪的问题
1: create-react-app my-app --typescript 无法创建一个TS的React项目
必须用create-react-app my-app --template typescript 命令
可能是版本关系吧

2:创建出的项目,无法识别tsx文件,报如下错误:

最后解决方法是:打开tsconfig.json, 找到"jsx": "react-jsx",改为"jsx": "react",再重新打开项目

奇怪的是,以前那台笔记本上,用create-react-app my-app --typescript创建的项目,默认都是"jsx": "react",而非"jsx": "react-jsx"

以下步骤演示了如何使用Material UI制作一个登录页面
create-react-app login --template typescript
cd login
npm install @material-ui/core --save
npm install @material-ui/icons --save

参考:https://material-ui.com/zh/getting-started/installation/

打开tsconfig.json, "jsx": "react-jsx" 改为:"jsx": "react"

新建SignIn.tsx,加入代码

import React from 'react';
import Avatar from '@material-ui/core/Avatar';
import Button from '@material-ui/core/Button';
import CssBaseline from '@material-ui/core/CssBaseline';
import TextField from '@material-ui/core/TextField';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Checkbox from '@material-ui/core/Checkbox';
import Link from '@material-ui/core/Link';
import Grid from '@material-ui/core/Grid';
import Box from '@material-ui/core/Box';
import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
import Typography from '@material-ui/core/Typography';
import { makeStyles, Theme } from '@material-ui/core/styles';
import Container from '@material-ui/core/Container';


function Copyright() {
  return (
    <Typography variant="body2" color="textSecondary" align="center">
      {'Copyright © '}
      <Link color="inherit" href="https://material-ui.com/">
        Your Website
      </Link>{' '}
      {new Date().getFullYear()}
      {'.'}
    </Typography>
  );
}

const useStyles = makeStyles((theme: Theme) => ({
  paper: {
    marginTop: theme.spacing(8),
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
  },
  avatar: {
    margin: theme.spacing(1),
    backgroundColor: theme.palette.secondary.main,
  },
  form: {
    width: '100%', // Fix IE 11 issue.
    marginTop: theme.spacing(1),
  },
  submit: {
    margin: theme.spacing(3, 0, 2),
  },
}));

export default function SignIn() {
  const classes = useStyles();

  return (
    <Container component="main" maxWidth="xs">
      <CssBaseline />
      <div className={classes.paper}>
        <Avatar className={classes.avatar}>
          <LockOutlinedIcon />
        </Avatar>
        <Typography component="h1" variant="h5">
          Sign in
        </Typography>
        <form className={classes.form} noValidate>
          <TextField
            variant="outlined"
            margin="normal"
            required
            fullWidth
            id="email"
            label="Email Address"
            name="email"
            autoComplete="email"
            autoFocus
          />
          <TextField
            variant="outlined"
            margin="normal"
            required
            fullWidth
            name="password"
            label="Password"
            type="password"
            id="password"
            autoComplete="current-password"
          />
          <FormControlLabel
            control={<Checkbox value="remember" color="primary" />}
            label="Remember me"
          />
          <Button
            type="submit"
            fullWidth
            variant="contained"
            color="primary"
            className={classes.submit}
          >
            Sign In
          </Button>
          <Grid container>
            <Grid item xs>
              <Link href="#" variant="body2">
                Forgot password?
              </Link>
            </Grid>
            <Grid item>
              <Link href="#" variant="body2">
                {"Don't have an account? Sign Up"}
              </Link>
            </Grid>
          </Grid>
        </form>
      </div>
      <Box mt={8}>
        <Copyright />
      </Box>
    </Container>
  );
}

修改App.tsx


import React from 'react';
import logo from './logo.svg';
import './App.css';

import SignIn from './components/SignIn'

function App() {
  return (
    <SignIn />
  );
}

export default App;

最终效果如下图:

posted @ 2020-11-26 19:43  老胡Andy  阅读(2719)  评论(0编辑  收藏  举报