为什么changeUser的e参数不能打印出来?
来源:6-2 登录功能实现(2)
leepulse
2021-06-25 23:18:10
import React, { Component } from 'react'
import { Modal, Button, Input } from 'antd';
import './style.css'
// import { Card } from 'antd';
import axios from 'axios';
class Login extends Component {
constructor(props) {
super(props)
this.showModal = this.showModal.bind(this)
this.hideModal = this.hideModal.bind(this)
this.changeUser = this.changeUser.bind(this)
this.state = {
login: false,
modal: false,
user: '',
password: ''
}
}
showModal() {
this.setState({
modal: true
})
}
hideModal() {
this.setState({
modal: false
})
}
changeUser(e) {
console.log(e);
}
render() {
const { login } = this.state
return (
<div className='login'>
{
login ? <Button type="primary">退出</Button> : <Button type="primary" onClick={this.showModal}>登录</Button>
}
<Modal
title="登录"
visible={this.state.modal}
onOk={() => { }}
onCancel={this.hideModal}
>
<Input
placeholder="请输入用户名"
type='text'
style={{ marginBottom: 10 }}
value={this.state.user}
onChange={this.changeUser}
/>
<Input
placeholder="请输入密码"
type='password'
value={this.state.password}
/>
</Modal>
</div>
)
}
componentDidMount() {
axios.get('http://www.dell-lee.com/react/api/isLogin.json').then(res => {
const login = res.data.data.login
this.setState({ login })
})
}
}
export default Login
1回答
同学你好,老师在源码中测试同学粘贴的代码,由于没有给密码输入框添加onChange事件,点击“登录”弹出输入框后控制条会出现报错,如下:
给密码输入框添加上onChange事件就不会有报错了,如下:
此时测试同学粘贴的代码,控制台可以正常输出changeUser函数中的e,如下:
建议同学清除浏览器缓存,重启项目后再测试下,祝学习愉快~
相似问题