【发布时间】:2018-12-08 00:12:12
【问题描述】:
我正在尝试按照本教程学习 ReactJS: Tutorial
我是编程语言的新手,所以我不知道现在该做什么。
当我尝试添加“Fetchemployee.tsx”文件时,this.state 方法出现错误。
(TS) 类型“FetchPeriod”上不存在属性“状态”
这是代码:
import * as React from 'react';
import { RouteComponentProps } from 'react-router';
import { Link, NavLink } from 'react-router-dom';
interface FetchPeriodDataState {
periodList: PeriodData[];
loading: boolean;
}
export class FetchPeriod extends React.Component<RouteComponentProps<{}>, FetchPeriodDataState> {
constructor(props) {
super(props);
this.state = { periodList: [], loading: true };
fetch('api/Period/Index')
.then(response => response.json() as Promise<PeriodData[]>)
.then(data => {
this.setState({ periodList: data, loading: false });
});
// This binding is necessary to make "this" work in the callback
this.handleDelete = this.handleDelete.bind(this);
this.handleEdit = this.handleEdit.bind(this);
}
后来我有了 PeriodData 类:
export class PeriodData {
PeriodId: number = 0;
Description: string = "";
PeriodOwner: string = "";
PeriodName: string = "";}
this.state 和 this.setState 方法给出了标题中的错误,我似乎找不到修复方法。
【问题讨论】:
-
你安装了 React 类型吗?
@types/react -
你确定构造函数中的状态初始化行给你错误吗?发布给出错误的确切行(以及上下文)
-
很抱歉,我在哪里可以查看?在 Package.json 中?
-
是的,请确保您在
dependencies或devDependencies中的package.json中列出了@types/react。 -
嗨 Cristy,请看下图:Link
标签: reactjs typescript react-router react-props