0
任何人都可以在react-js上下文中向我解釋以下場景: 我使用webpack並使用預設「babel-preset-env」&「react」。Render json沒有在React中定義
在文件的頂部,我導入一個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;
}
}
任何幫助,非常感謝。
謝謝!那是個很好的觀點。當我將它添加到類構造函數this.config = config中的模塊中時,我可以設法訪問配置。但爲什麼console.log可以訪問CONF變量? – doemsche
'console.log'可以「看到」它,因爲它在* CONFIG的範圍內(即在模塊內部)運行*。如果你嘗試在另一個模塊/文件/組件中使用'console.log(CONFIG)',它會失敗:可能會打印'undefined'或拋出相同的錯誤。 –