在根目录下的文件app1.js,app2.js,app3.js中引入todos目录下的reducer文件
- 传统做法:在app1.js,app2.js,app3.js文件中都分别引入todos目录下的reducer.js文件中的reducer模块。
import {reducer as todoReducer} from './todos/reducer.js';
然而这样的引入方法存在一个巨大的隐患,如果有一天我给reducer文件换了个名字叫reducerChange,那么我需要在app1.js,app2.js,app3.js中都修改一次。为了避免这样枯燥而繁琐的工作,我们采用新的模块引入方式
- 新做法:在todos目录下创建一个index.js作为该目录下所有模块的接口。
todos/index.js
import reducer from './reducer.js';
export {actions, reducer, view};
再在app1.js,app2.js,app3.js文件中通过todos/index.js引入模块
//index.js被省略
import {reducer as todoReducer} from './todos';
import {reducer as filterReducer} from './filter';
采用新做法后,我们发现,修改reducer文件名后只需在todos文件夹下的index.js中做一次修改即可。
import reducer from './reducerChange.js';
export {actions, reducer, view};