React事件的正确类型是什么.最初我只是any
为了简单起见而使用.现在,我正在努力清理并避免any
完全使用.
所以用这样一个简单的形式:
export interface LoginProps {
login: {
[k: string]: string | Function
uname: string
passw: string
logIn: Function
}
}
@inject('login') @observer
export class Login extends Component<LoginProps, {}> {
update = (e: React.SyntheticEvent<EventTarget>): void => {
this.props.login[e.target.name] = e.target.value
}
submit = (e: any): void => {
this.props.login.logIn()
e.preventDefault()
}
render() {
const { uname, passw } = this.props.login
return (
<div id='login' >
<form>
<input
placeholder='Username'
type="text"
name='uname'
value={uname}
onChange={this.update}
/>
<input
placeholder='Password'
type="password" …
Run Code Online (Sandbox Code Playgroud) 所以下面的代码是在Angular 4中,我无法弄清楚为什么它没有像预期的那样工作.
这是我的处理程序的片段:
onUpdatingServerName(event: Event) {
console.log(event);
this.newserverName = event.target.value; //this wont work
}
Run Code Online (Sandbox Code Playgroud)
HTML元素:
<input type="text" class="form-control" (input)="onUpdatingServerName($event)">
Run Code Online (Sandbox Code Playgroud)
代码给了我错误:
'EventTarget'类型中不存在属性'value'.
但正如可以看出console.log
,价值确实存在于event.target
.