【问题标题】:Uncaught ReferenceError: Cannot access 'CONFIG' before initialization未捕获的 ReferenceError:在初始化之前无法访问“CONFIG”
【发布时间】:2020-06-02 01:12:04
【问题描述】:

CRA 创建的项目。

utils/config.js

   CONFIG={
    test:'test
   }

然后我在我的 src/index.js(这是项目的根目录)中导入

    <Provider store={store}>
            <Router>
                <Switch>
                    <Route path='/' exact>
                        <MainContainer title={'Welcome to something'} />
                    </Route>
                    <Route path='/dummy' exact>
                        <DummyContainer />
                    </Route>
                    <Route path='/pop/:topicId' exact>
                        <Popup />
                    </Route>
                    <Route path='/app' exact>
                        <App />
                    </Route>
                </Switch>
            </Router>
        </Provider>,

导入'utils/config';

我可以从 src/index.js 调用的组件访问 CONFIG

但发生了无法访问 CONFIG 的错误。

有没有办法将 CONFIG 置于第一个状态?

谢谢。

【问题讨论】:

    标签: javascript node.js reactjs config


    【解决方案1】:

    确保您正确导出它。它应该是默认或命名导出。 尝试以下更改

    utils/config.js

    export default {
      test:'test'
    }
    

    src/index.js(项目的根目录)

    import config from 'utils/config'
    

    现在您可以随意使用 config 对象

    【讨论】:

    • 嗯...实际上我想使用从 src/index.js 调用的子组件中的“测试”。还是谢谢你!
    猜你喜欢
    • 2020-12-12
    • 2020-05-06
    • 2021-03-10
    • 2020-06-02
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    相关资源
    最近更新 更多