Reactjs 错误TS2339:类型“Readonly”上不存在属性“items”。usnig在SPX中反应
我正在使用react处理一个SPFX问题,但不断出现错误:- 错误-typescript-src\webparts\reactReadWebpart\components\reactReadWebpart.tsx101,25:错误TS2339:类型“Readonly”上不存在属性“items”。 请参见下面的屏幕截图Reactjs 错误TS2339:类型“Readonly”上不存在属性“items”。usnig在SPX中反应,reactjs,spfx,Reactjs,Spfx,我正在使用react处理一个SPFX问题,但不断出现错误:- 错误-typescript-src\webparts\reactReadWebpart\components\reactReadWebpart.tsx101,25:错误TS2339:类型“Readonly”上不存在属性“items”。 请参见下面的屏幕截图 export default class ReactReadWebpart extends React.Component<IReactReadWebpartProps,
export
default class
ReactReadWebpart
extends
React.Component<IReactReadWebpartProps, {}> {
public
constructor(props:
IReactReadWebpartProps,
state:
IReactReadWebpartState){
super(props);
this.state
= {
items: [
{
"EmployeeName":
"",
"EmployeeId":
"",
"Experience":"",
"Location":""
}
]
};
}
在下面的state.items处出错
{this.state.items.map(function(item,key){
return (<div
className={styles.rowStyle}
key={key}>
<div
className={styles.CellStyle}>{item.EmployeeName}</div>
<div
className={styles.CellStyle}>{item.EmployeeId}</div>
<div
className={styles.CellStyle}>{item.Experience}</div>
<div
className={styles.CellStyle}>{item.Location}</div>
</div>);
})}
提前感谢将问题中的一级代码格式设置在一边,您将得到错误,因为组件的状态在这一行中描述为空对象
React.Component<IReactReadWebpartProps, {}>
所以您可能应该定义它,这样typescript就知道会发生什么。一些类型或接口将包含items字段即可。谢谢Daniel,但我已经有了此接口导出接口IReactReadWebpartState{items:[{EmployeeName:,EmployeeId:,Experience:,Location:}]}如果您能帮助的话,这仍然非常新!非常好!您的接口不是完全正确的,它应该类似于接口项{EmployeeName:string;EmployeeId:string}接口IReactReadWebpartState{items:Item[];}。然后,您必须将其作为类React.Component的第二个泛型类型参数来提及。另外,一般建议不要在typescript中的接口前面加I,因为它不会给接口增加任何额外的意义!祝你好运谢谢你,丹尼尔。我会试试看