最佳答案
我刚接触打字机。我在渲染方法中显示 this.state.something
或者在函数中将它赋值给变量时遇到了问题。
看看最重要的一段代码:
interface State {
playOrPause?: string;
}
class Player extends React.Component {
constructor() {
super();
this.state = {
playOrPause: 'Play'
};
}
render() {
return(
<div>
<button
ref={playPause => this.playPause = playPause}
title={this.state.playOrPause} // in this line I get an error
>
Play
</button>
</div>
);
}
}
错误显示: [ts] Property 'playOrPause' does not exist on type 'ReadOnly<{}>'.
我尝试将 playOrPuse 属性声明为一种字符串类型,但是它不起作用。
我到底错过了什么?