不洗碗工做室-wangpengcss
### 腳手架選擇html
選擇不洗碗工做室的react腳手架react
git clone https://github.com/ouxu/NEUQer-FE-Kit.git
複製代碼
打開其中的React-Route4在終端輸入git
npm install
複製代碼
會根據package.json安裝必要的依賴github
再安裝Ant designnpm
npm install antd --save
複製代碼
其中--save將把antd自動加入到package.json文件中json
開啓本地預覽後端
npm run dev
複製代碼
修改 src/componments/Layout/index.js, 引入antdbash
import { Form, Icon, Input, Button, Checkbox } from 'antd';
複製代碼
使用fetch完成與後端的數據交互部分
handleSubmit = (e) => {
e.preventDefault()
console.log(2)
this.setState({loading: true})
console.log(3)
this.props.form.validateFieldsAndScroll((err, values) => {
if (err) {} else {
message.loading('提交成功,正在驗證')
const body = {
...values
}
fetch('api', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}).then((res) => {
return res.json()
}).then((json) => {
if (json.code === 0) {
message.success('success')
} else {
message.error('failed')
}
}).catch((e) => {
console.log(e.message)
})
}
})
}
複製代碼
登陸框頁面組織
<Form onSubmit={this.handleSubmit} className="login-form">
<FormItem>
{getFieldDecorator('userName', {
rules: [{ required: true, message: 'Please input your username!' }],
})(
<Input prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />} placeholder="Username" />
)}
</FormItem>
<FormItem>
{getFieldDecorator('password', {
rules: [{ required: true, message: 'Please input your Password!' }],
})(
<Input prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} type="password" placeholder="Password" />
)}
</FormItem>
<FormItem>
{getFieldDecorator('remember', {
valuePropName: 'checked',
initialValue: true,
})(
<Checkbox>Remember me</Checkbox>
)}
<a className="login-form-forgot" href="">Forgot password</a>
<Button type="primary" htmlType="submit" className="login-form-button">
Log in
</Button>
Or <a href="">register now!</a>
</FormItem>
</Form>
複製代碼
頁面的CSS佈局
#components-form-demo-normal-login .login-form {
max-width: 300px;
}
#components-form-demo-normal-login .login-form-forgot {
float: right;
}
#components-form-demo-normal-login .login-form-button {
width: 100%;
}
複製代碼
最後組織成的index.js文件
import React, { Component} from 'react'
import request from 'utils/request'
import './index.css'
import 'antd/lib/button/style';
import { Form, Icon, Input, Button, Checkbox } from 'antd';
const FormItem = Form.Item;
@Form.create()
class HomePage extends Component {
handleSubmit = (e) => {
e.preventDefault()
console.log(2)
this.setState({loading: true})
console.log(3)
this.props.form.validateFieldsAndScroll((err, values) => {
if (err) {} else {
message.loading('提交成功,正在驗證')
const body = {
...values
}
fetch('api', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}).then((res) => {
return res.json()
}).then((json) => {
if (json.code === 0) {
message.success('success')
} else {
message.error('failed')
}
}).catch((e) => {
console.log(e.message)
})
}
})
}
render() {
const { getFieldDecorator } = this.props.form;
return (
<Form onSubmit={this.handleSubmit} className="login-form">
<FormItem>
{getFieldDecorator('userName', {
rules: [{ required: true, message: 'Please input your username!' }],
})(
<Input prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />} placeholder="Username" />
)}
</FormItem>
<FormItem>
{getFieldDecorator('password', {
rules: [{ required: true, message: 'Please input your Password!' }],
})(
<Input prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} type="password" placeholder="Password" />
)}
</FormItem>
<FormItem>
{getFieldDecorator('remember', {
valuePropName: 'checked',
initialValue: true,
})(
<Checkbox>Remember me</Checkbox>
)}
<a className="login-form-forgot" href="">Forgot password</a>
<Button type="primary" htmlType="submit" className="login-form-button">
Log in
</Button>
Or <a href="">register now!</a>
</FormItem>
</Form>
);
}
}
export default HomePage
複製代碼
至此就可完成一個登陸功能的實現