项目地址:github.com/Nealyang/Re…前端
本想等项目作完再连载一波系列博客,随着开发的进行,也是的确遇到了很多坑,请教了很多人。遂想,何不一边记录踩坑,一边分享收获呢。分享固然是好的, 若是能作到集思广益,那岂不是更美。咱们的口号是:坚定不会烂尾react
本博客为连载代码博客同步更新博客,随着项目日后开发可能会遇到前面写的不合适的地方会再回头修改。若有不妥~欢迎兄弟们不啬赐教。谢谢!git
首先咱们在项目/app/reducers下新建一个index.js,用于导出全部的reducer。 也用于将admin、front等reducer汇总的文件。最后combineReducers后直接导出。github
export default combineReducers({
front,
globalState: reducer,
admin
})
复制代码
上面admin,reducer,front是别的文件中的reducer处理。这里咱们能够暂时导出一个空的state。express
对应的每个子reducer书写大体以下:redux
export const actionTypes = {
ADMIN_URI_LOCATION:"ADMIN_URI_LOCATION"
};
const initialState = {
url:"/"
};
export const actions = {
change_location_admin:function (url) {
return{
type:actionTypes.ADMIN_URI_LOCATION,
data:url
}
}
};
export function reducer(state=initialState,action) {
switch (action.type){
case actionTypes.ADMIN_URI_LOCATION:
return {
...state,url:action.data
};
default:
return state
}
}
const admin = combineReducers({
adminGlobalState:reducer,
users,
tags
});
export default admin
复制代码
定义initialState(这个state节点上的initialState,不总的state),actions,actionTypes以及reducer。而后倒入reducer。 在index中引入后,即为state中的admin节点。后端
配置store的文件。这个文件的功能正如其名,就是配置store的。其中咱们主要作了以下工做。微信
代码以下:session
const win = window;
const sagaMiddleware = createSagaMiddleware();
const middlewares = [];
let storeEnhancers ;
if(process.env.NODE_ENV==='production'){
storeEnhancers = compose(
applyMiddleware(...middlewares,sagaMiddleware)
);
}else{
storeEnhancers = compose(
applyMiddleware(...middlewares,sagaMiddleware),
(win && win.devToolsExtension) ? win.devToolsExtension() : (f) => f,
);
}
export default function configureStore(initialState={}) {
const store = createStore(rootReducer, initialState,storeEnhancers);
sagaMiddleware.run(rootSaga);
if (module.hot && process.env.NODE_ENV!=='production') {
// Enable Webpack hot module replacement for reducers
module.hot.accept( './reducers',() => {
const nextRootReducer = require('./reducers/index');
store.replaceReducer(nextRootReducer);
});
}
return store;
}
复制代码
最后倒入store,用于在App中使用。react-router
react-router中的配置主要在/container/index.js文件中。该文件负责导出全部路由中的文件。
说一下该项目的路由大体规则。默认状况下,输入域名(咱们这里是localhost),直接进入首页。也就是咱们项目中的front部分。
/ -> 首页 (虽说首页,可是仔细看页面结构,其实就是文章列表页)
/:tag -> 其余文章列表页
/detail/:id -> 详情页
/admin -> 后台管理首页
/admin/xxx -> 后台管理页的某一个模块 好比:/admin/managerTags -> 标签管理页面
/404 -> notFound
复制代码
因此根据路由配置先具体后模糊的规则。而且这里牵涉到路由嵌套,因此一定抽离出组件来:
index.js render部分以下:
render() {
let {isFetching} = this.props;
return (
<Router>
<div>
<Switch>
<Route path='/404' component={NotFound}/>
<Route path='/admin' component={Admin}/>
<Route component={Front}/>
</Switch>
{isFetching && <Loading/>}
{this.props.notification && this.props.notification.content ?
(this.props.notification.type === 1 ?
this.openNotification('success', this.props.notification.content) :
this.openNotification('error', this.props.notification.content)) :
null}
</div>
</Router>
)
}
复制代码
由于路由模糊的部分只要front部分是最模糊的,因此咱们把它匹配到最下面。别的你们应该都没有疑惑,至于isFetch notification后面会介绍。 至于为何要在这里放这些isFetch和notification。由于这是全部路由的最外面一层,是front和admin界面下公共的部分。Loading加载图标,全局提示信息均可以公用。 因此咱们放在最外层。
必定记住。能公用的一组东西,咱们必定是放到路由匹配的最外层。
下面看下Font和admin的代码
const Front = ({match}) => {
return (
<div>
<div className={`${animationStyle.animated} ${animationStyle.fadeInDown}`}>
<Banner/>
<Menus/>
</div>
<Switch>
<Route exact path={match.url} component={Home}/>
<Route path={`/detail/:id`} component={Detail}/>
<Route path={`/:tag`} component={Home}/>
<Route component={NotFound}/>
</Switch>
</div>
)
};
复制代码
admin:
render() {
const {url} = this.props.match;
if(this.props.userInfo.userType){
return (
<div>
{
this.props.userInfo.userType === 'admin' ?
<div className={style.container}>
<div className={style.menuContainer}>
<AdminMenu history={this.props.history}
url={this.props.adminUrl}
changeUrl={this.props.change_location_admin}/>
</div>
<div className={style.contentContainer}>
<Switch>
<Route exact path={url} component={AdminIndex}/>
<Route path={`${url}/managerUser`} component={AdminManagerUser}/>
<Route path={`${url}/managerTags`} component={AdminManagerTags}/>
<Route path={`${url}/newArticle`} component={AdminNewArticle}/>
<Route path={`${url}/detail`} component={Detail}/>
<Route component={NotFound}/>
</Switch>
</div>
</div> :
<Redirect to='/'/>
}
</div>
)
}else{
return <NotFound/>
}
}
复制代码
注意admin中的路由匹配,这里必需要使用{match},不然你点击link你会发现路由跳转成功了,可是对应页面没有渲染。
关于admin中为何判断this.props.userInfo后续权限判断哪里会说到。以及会说这里遇到的一些问题(重点)。这里咱们仍是只关注路由部分。再次强调,必须使用match 来取url。而后根据本身后台管理的定义项,随着开发,日后面去填充对应的路由便可。
至此,这个项目的redux,router基本就配置完成了。后续随着开发,回往/app/reducers中在添加对应的reducer。以及在路由中添加新建的页面。
若是您有更好想法,欢迎您联系我。咱们一块儿改进~
若是有什么不明白的地方,欢迎提issue。我会第一时间处理。
欢迎关注我的微信公众号: Nealyang 全栈前端,获取第一手文章推送和免费全栈电子书分享福利