React + react-router + react-redux前端架构

本文介绍了如何使用create-react-app初始化项目,然后详细讲解了如何配置和使用react-router4.0实现路由管理,包括路由嵌套和传参。接着,探讨了react-redux的安装和应用,包括store的创建、reducers和actions的编写,以及组件和方法中如何使用redux。最后,文章提到了基于axios的全局请求封装和错误处理,以及项目的目录结构。
摘要由CSDN通过智能技术生成

首先安装creact-react-app

npm i create-react-app -g

创建APP

create-react-app my-app

cd my-app

先执行 npm run eject生成webpack配置文件

Router安装

npm i react-router -s (npm i react-router@3.2.1 -s)

react-router4.0

提供了Router,Route组件

import {Router,Route} from 'react-router';

import createHashHistory from "history/createHashHistory";

    <Router history={history}>

        <Route path="/" component={App}></Route>

    </Router>

路由嵌套 router4.0嵌套路由写在对应父组件内部

react-router3.0

import {Router,useRouterHistory} from 'react-router';
import {syncHistoryWithStore} from 'react-router-redux';
import {createHashHistory} from "history";
import routes from './containers/routerRegister';

import store from './store';

const routeHistory = useRouterHistory(createHashHistory)();
const history = syncHistoryWithStore(routeHistory, store);

const rootRoute = {
    path:'/',
    component: App,
    childRoutes: routes
};

<Provider store={store}>
    <Router history={history} routes={rootRoute}></Router>
</Provider>

对应的子路由childRoutes

import homeRouter from './Home/router';
import userRouter from './User/router';

const routes = []
	.concat(homeRouter)
	.concat(userRouter);

export default routes;

对应的子路由(如果要实现按需加载需要通过module.export导出一个模块,在routerRegister中使用require()的方式引入对应模块,在webpack中需要修改配置项,此处需要自己百度一波)

import Home from './index';

const routes = [
	{
    	path: 'home',
    	component: Home,
    	// getComponent(nextState, cb) {
    	//     require.ensure([], (require) => {
        //         cb(null, require('./EastPrescribe'))
        //     })
        // 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值