我正在尝试使用对 JSON.server 的直接获取来检索基本包以加载一些简单的组件。当我在 Firefox 中运行这个脚本时。我在尝试获取资源时收到“网络错误”,但在网络连接本身中找不到任何 400 错误。我的问题是,什么会导致这种故障?因为页面的其余部分在调用时加载正常。
编辑 1:它特别说明 data.StatusComponent 未定义。
import React, { Component } from 'react';
import '../HeaderBar/HeaderBar.css';
import 'whatwg-fetch';
type StatusBarProps = {};
type StatusBarState = {
launch_timer: boolean;
foreGroundTimer: number;
// tslint:disable-next-line:no-any
statusBarBlocks: any;
};
class StatusBar extends Component<StatusBarProps, StatusBarState> {
timerId: number;
constructor() {
super();
this.state = {
launch_timer: true,
foreGroundTimer: -1,
statusBarBlocks: []
};
}
componentDidMount() {
fetch('http://localhost:5001/StatusComponent').then(results => {
return results.json();
}).then(data => {
let systemOff = 'green';
let systemOn = 'gray';
let statusBarBlocks = data.StatusComponent.map((Status) => {
return (
<div className="blockBar" id={Status.id} key={Status.key} style={{ backgroundColor: (this.state.foreGroundTimer >= Status.id) ? systemOff : systemOn }}> {Status.Name} </div>
);
});
this.setState({ statusBarBlocks: statusBarBlocks });
});
}
componentWillUnmount() {
clearInterval(this.timerId);
}
tick() {
this.setState({ launch_timer: !this.state.launch_timer, foreGroundTimer: (this.state.foreGroundTimer + 1) % 26 });
}
render() {
return (
<div className="location">
<div className="col-xs-6">
{this.state.statusBarBlocks}
</div>
</div>
);
}
}
export default StatusBar;