有人能在react-js上下文中向我解释以下场景:我与webpack一起工作,并使用预设"babel-preset-env“和"react”。
在一个文件的顶部,我导入了一个config.json,并尝试使用开发人员工具和调试器语句来检查它。
console.log会按预期记录一个对象数组。如果我进入developer-tools js-console并输入ReferenceError,我会得到一个未捕获的配置: CONFIG is not defined。
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;
}
}任何帮助都是非常感谢的。
发布于 2017-08-16 15:59:16
CONFIG在您正在编写的模块中定义。它不是一个真正的全局变量,它只是模块(即那个文件)中的“全局”变量。
如果你真的想让它在浏览器中全局可用,可以尝试添加window.CONFIG = CONFIG。
https://stackoverflow.com/questions/45707957
复制相似问题