我在asp.net核心反应应用程序中创建了以下类:
import * as React from 'react';
interface MyInputProps {
inputType: string;
id: string;
className: string;
parentFunction: (string) => void;
}
export class LoginInput extends React.Component<MyInputProps, {}> {
constructor() {
super();
this.onChange = this.onChange.bind(this);
}
private onChange(e) {
this.props.parentFunction(e.target.value);
}
public render() {
return <input type={this.props.inputType} id={this.props.id} className={this.props.className} placeholder="username" onChange={this.onChange} />;
}
}
现在我收到以下错误:
有谁能指出我在这里做错了什么?
修改
在另一个解决方案中,我有以下类可以正常工作:
import * as React from 'react';
import axios from 'axios';
import Country from './Country';
import CountryModel from './models/CountryModel';
const countriesUri = 'https://restcountries.eu/rest/v2/all?fields=name;alpha2Code';
interface Props {
onChange: (string) => void;
}
interface State {
countries: CountryModel[];
}
class CountryList extends React.Component<Props, State> {
constructor() {
super();
this.state = {
countries: []
};
this.onChange = this.onChange.bind(this);
}
componentWillMount() {
this.getCountries();
}
private getCountries() {
//
}
private onChange(e) {
this.props.onChange(e.target.value);
}
public render() {
return <select onChange={this.onChange} ref='countries'>
{
//
}
</select>;
}
}
export default CountryList;
答案 0 :(得分:1)
您必须定义变量的类型,例如
interface MyInputProps {
parentFunction: (string:string) => void;
}
private onChange(e:any) {
this.props.parentFunction(e.target.value);
}
错误消息基本上意味着如果您不定义类型,它将隐式地向其添加:any
。检查的原因是noImplicitAny: true
tsconfig.json