渲染json未在React中定义

时间:2017-08-16 07:50:23

标签: javascript reactjs

任何人都可以在react-js上下文中向我解释以下场景: 我使用webpack并使用预设" babel-preset-env" &安培; "反应"

在文件的顶部,我导入了一个config.json,我尝试使用开发人员工具和调试器语句进行检查。

console.log按预期记录对象数组。如果我进入developer-tools js-console并输入CONFIG,我会得到一个Uncaught ReferenceError:CONFIG未定义。

import React, { Component } from 'react';
import CONFIG from './config.json';

class MyComponent extends Component{
    render(){
        //this statement logs as expected
        console.log(CONFIG);

        // the debugger stops execution, but when I enter CONFIG in the
        // dev-tools Console I get the error: Uncaught ReferenceError: 
        // CONFIG is not defined
        debugger;
    }
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

CONFIG在您编写的模块中定义。它不是一个真正的全局变量,它只是该模块内的“全局”(即该文件)。

如果您确实想要在浏览器中全局使用它,请尝试添加window.CONFIG = CONFIG

相关问题