javascript - typescript 错误 : Cannot redeclare block-scoped variable 'fetch'

标签 javascript json reactjs typescript fetch-api

我正在进行概念验证,以从 JSON 服务器获取一些简单的 JSON 数据以显示在我的 React 应用程序中。在这样做时,我试图调用 fetch 来直接加载数据。当我这样做时。

ERROR: "cannot redeclare block-scoped variable 'fetch'.

我的问题是,在使用常规 Typescript 时,我该如何解决这个问题?

import React, { Component } from 'react';
import '../HeaderBar/HeaderBar.css';

type StatusBarProps = {};
type StatusBarState = {
    launch_timer: boolean;
    foreGroundTimer: number;

};
class StatusBar extends Component<StatusBarProps, StatusBarState> {
    timerId: number;
    constructor() {
        super();
        this.state = {
            launch_timer: true,
            foreGroundTimer: -1,
        };
    }
    componentDidMount() {
        window.fetch('localhost:3000/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;

编辑 1:问题的位置是@types/whatwg-fetch/index.d.ts,这让我相信这是 typescript 的问题。我实现了 Isomorphic-fetch 但无济于事。

最佳答案

所以在努力发现这个问题的原因。我发现我在 Lib 文件和 @types/whatwg-fetch 中都声明了 fetch 的地方有重叠。卸载此依赖项允许程序编译。解决问题。

whatwg-fetch new typescript 2.5.3

在这个 SO 帖子中找到。感谢芬顿的回答。

关于javascript - typescript 错误 : Cannot redeclare block-scoped variable 'fetch' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47083772/

相关文章:

reactjs - Gulp + Browserify + Babelify意外 token

javascript - Nodejs elementtree npm xml解析

php - 如何自动清除浏览器缓存?

mysql - JSON Rails 一对多的嵌套属性

java - Jackson:自定义反序列化器:无法使用数组反序列化 XML

reactjs - 更新前端帖子中的 Like 数组

javascript - 使用 react 调用异步函数

javascript - 早午餐设置 LOGGY_STACKS=true

javascript - ngView 中指令使用的远程数据存储在哪里? (AngularJS)

c# - 在 MVC 应用程序中使用 await Task<T> async 挂起 Controller