React

React(定义模块接口)

2017-10-27  本文已影响10人  余生筑

在根目录下的文件app1.js,app2.js,app3.js中引入todos目录下的reducer文件

import {reducer as todoReducer} from './todos/reducer.js';

然而这样的引入方法存在一个巨大的隐患,如果有一天我给reducer文件换了个名字叫reducerChange,那么我需要在app1.js,app2.js,app3.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};
上一篇下一篇

猜你喜欢

热点阅读