ES6模块初始化顺序错误

ES6 modules initializing the wrong order?

本文关键字:错误 顺序 初始化 模块 ES6      更新时间:2023-09-26

所以我有一个模块树,特别是两个表现异常的模块。使用Babel 6+Browserify(babelify)。

模块#1(WindowManager/index.js)导出一个名为reducer的命名函数,以及一个默认导出。

import WindowManager from './component';
import { combineReducers } from 'redux';
import { connect } from 'react-redux';
import { reducer as search_palette } from './SearchPalette';
const initialState = {
    // stuff
};
const window_manager = (state = initialState, action) => {
    // stuff
    return state;
};
export const reducer = combineReducers({window_manager, search_palette})
const mapStateToProps = (state) => {
    return {
        // stuff
    };
};
export default connect(mapStateToProps)(WindowManager)

模块#2(store.js)导入WindowManager/index.jsreducer函数并尝试使用它

import { createStore, combineReducers } from 'redux';
import { reducer as WindowManagerReducer } from './WindowManager';
const initialState = {
    // stuff
};
const FetchApplicationsReducer = (state = initialState, action) => {
    // stuff
    return state;
}
export default createStore(
    combineReducers({
        applications: FetchApplicationsReducer,
        ui: WindowManagerReducer,
    })
)

由于某种原因,store.js在WindowManager/index.js之前执行,因此导出不可用。

有人碰到这个吗?

经过多次调整,修改import语句的顺序解决了这个问题。这是一棵复杂的树,很难在这里以一种不涉及整个项目的方式来表示。。。但我认为最终Babel的ES6模块系统中可能存在漏洞。