react-router: 2.X版本的react-router的相关配置

AppRouter.js

import React, {Component} from 'react';
import {
  Router,
  Route,
  useRouterHistory,
  IndexRoute,
  IndexRedirect
} from 'react-router';
import {createHashHistory} from 'history';

import Master from './Master';

import aMainPage from '../pages/a/main';
import A1Page from '../pages/a/a1';
import A2Page from '../pages/a/a2';
import A3Page from '../pages/a/a3';


import bMainPage from '../pages/b/main';
import B1Page from '../pages/b/b1';
import B2Page from '../pages/b/b2';

import cMainPage from '../pages/c/main';
import C1Page from '../pages/b/b1';

import dMainPage from '../pages/d/main';

import NotFound from './errors/NotFound';

class AppRouter extends Component {
  render() {
    //由于使用的是hashHistory,为了去掉url中?符号后面的无关字符,所以使用使用外部的 history 模块
    const appHistory = useRouterHistory(createHashHistory)({ queryKey: false });
    return (
      <Router
        history={appHistory}
        onUpdate={() => window.scrollTo(0, 0)}
      >
         //组件Master是主页面组件,一般情况下是一个layout,假设这个页面使用侧边栏进行页面切换,则aMainPage,bMainPage,cMainPage,dMainPage这四个页面组件会作为Master的同级的this.props.children分别按路由渲染
        <Route path="/" component={Master} >
          //IndexRedirect的作用是指定一个路由地址作为跳转地址
          <IndexRedirect to="a" />
          <Route path="a" >
            //第一层级页面
            //IndexRoutede的作用是指定一个组件作为默认页
            <IndexRoute component={aMainPage} />
            <Route path=":id">
              //第二层级页面
              <IndexRoute component={A1Page} />
              //第三层级页面
              <Route path="a1" component={A2Page} />
              //第三层级页面
              <Route path="a2" component={A3Page} />
            </Route>
          </Route>
          
          <Route path="b" >
            //第一层级页面
            <IndexRoute component={bMainPage} />
            //第二层级页面
            <Route path="b1" component={B1Page} />
            //第二层级页面
            <Route path=":id">
              <Route path="b2" component={B2Page} />
            </Route>
          </Route>

          <Route path="c" >
            //第一层级页面
            <IndexRoute component={cMainPage} />
            //第二层级页面
            <Route path="c1" component={C1Page} />
          </Route>
          
          //第一层级页面
          <Route path="d" component={dMainPage} />
    
          <Route path="*" component={NotFound} />
          
        </Route>
      </Router>
    );
  }
}

export default AppRouter;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值