reactjs - 获取错误参数“props”隐式具有“any”类型
问题描述
获取错误参数'props'隐式具有'any'类型。 我正在使用对材料 UI 做出反应,一切正常,但得到错误参数“道具”隐含地具有“任何”类型。我也在使用 axios API 来获取演示数据。我正在使用对材料 UI 做出反应,一切正常,但得到错误参数“道具”隐含地具有“任何”类型。我也在使用 axios API 来获取演示数据。
import React, { Component } from 'react'
import ApiService from "../../service/ApiService";
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
class AddUserComponent extends Component{
constructor(props){
super(props);
this.state ={
username: '',
password: '',
firstName: '',
lastName: '',
age: '',
salary: '',
message: null
}
this.saveUser = this.saveUser.bind(this);
}
saveUser = (e) => {
e.preventDefault();
let user = {username: this.state.username, password: this.state.password, firstName: this.state.firstName, lastName: this.state.lastName, age: this.state.age, salary: this.state.salary};
ApiService.addUser(user)
.then(res => {
this.setState({message : 'User added successfully.'});
this.props.history.push('/users');
});
}
onChange = (e) =>
this.setState({ [e.target.name]: e.target.value });
render() {
return(
<div>
<Typography variant="h4" style={style}>Add User</Typography>
<form style={formContainer}>
<TextField type="text" placeholder="username" fullWidth margin="normal" name="username" value={this.state.username} onChange={this.onChange}/>
<TextField type="password" placeholder="password" fullWidth margin="normal" name="password" value={this.state.password} onChange={this.onChange}/>
<TextField placeholder="First Name" fullWidth margin="normal" name="firstName" value={this.state.firstName} onChange={this.onChange}/>
<TextField placeholder="Last name" fullWidth margin="normal" name="lastName" value={this.state.lastName} onChange={this.onChange}/>
<TextField type="number" placeholder="age" fullWidth margin="normal" name="age" value={this.state.age} onChange={this.onChange}/>
<TextField type="number" placeholder="salary" fullWidth margin="normal" name="salary" value={this.state.salary} onChange={this.onChange}/>
<Button variant="contained" color="primary" onClick={this.saveUser}>Save</Button>
</form>
</div>
);
}
}
const formContainer = {
display: 'flex',
flexFlow: 'row wrap'
};
const style ={
display: 'flex',
justifyContent: 'center'
}
export default AddUserComponent;
解决方案
无需删除 tsconfig.json 中的 'noImplicitAny' - 您可以显式地为 props 赋予将删除错误的 'any' 类型:
constructor(props: any){
(来源:https ://www.tsmean.com/articles/learn-typescript/no-implicit-any-best-practice/ )
推荐阅读
- c# - c#桌面应用程序中的数据列表
- java - 如何启用/禁用移动数据下载
- python - TF 对象检测:int() 参数必须是字符串、类似字节的对象或数字,而不是 'NoneType'
- python - 时间序列的交叉验证
- python - Tkinter 将小部件放置在相对于窗口调整大小的特定位置
- sonarqube - 通过询问 sonar.java.binaries 属性从 5.6 升级到 Sonar 6.7.3 后构建失败
- javascript - 动态切换到不同的firebase项目数据库(Javascript)
- angular - Angular 5 EventSource 不发送会话 cookie
- javascript - 当用户单击浏览器中的后退按钮时,保存用户输入的最佳方法是什么?
- sonarqube - 升级到 7.1 后 Sonarqube ps 值错误