手写一个根据目录自动生成的路由

一、起因

最近研究了一下阿里dva的quickstart,其中路由配置是手动添加。
如下,先将要显示的页面导入router.js,然后配置 .
其中path="/products"是配置的路由地址,component={Products}为这个地址显示的页面。

import React from 'react';
import { Router, Switch, Route } from 'dva/router';
import Products from './routes/Products';
import Example from './components/Example';

function RouterConfig({ history }) {
  return (
    <Router history={history}>
      <Switch>
        <Route path="/" exact component={Example}/>
        <Route path="/products" exact component={Products}/>
      </Switch>
    </Router>
  );
}

module.exports = RouterConfig;

二、思索

想了一下,或许可以让路由自动生成,就不用每个都手动导入。
我的实现思路是,写一个配置文件router.config.js,让他自动生成路由地址及指向的模块。
改写后的router.js根据读取到的信息生成,而不再写死。改写后的router.js代码如下

import React from 'react';
import { Router, Route, IndexRoute } from 'dva/router';
import RouterArray from './router.config';
import NotFound from './routes/404';

const loadRoutes = (rts) => {
  const arr = [];
  rts.forEach((route, idx) => {
    if (route.children) {
      arr.push(
        route.component ?
          <Route key={route.path + idx} path={route.path} component={route.component}>
            {loadRoutes(route.children)}
          </Route>
          :
          <Route key={route.path + idx} path={route.path}>
            {loadRoutes(route.children)}
          </Route>
          ,
      );
    } else if (route.index === true) {
      arr.push(<IndexRoute key={idx} component={route.component} />);
    } else {
      arr.push(<Route key={route.path + idx} path={route.path} component={route.component} />);
    }
  });
  return arr;
};

function RouterConfig({ history }) {
  const routes = loadRoutes(RouterArray);
  routes.push(<Route key="404" path="/*" component={NotFound} />);
  return (
    <Router history={history}>
      { routes }
    </Router>
  );
}

export default RouterConfig;

其中的RouterArray是配置文件router.config.js根据目录读取到的信息,传给router.js,然后生成路由。router.config.js的代码如下:

import App from './routes/Main';

const routerConfig = [];
const files = require.context('./routes', true, /route.js$/);
files.keys().filter(i => i.includes('/route.js') || i.includes('/app.route.js')).forEach((key) => {
  const m = files(key);
  const idx = routerConfig.findIndex(it => it.path === m.path);
  if (idx > -1) {
    routerConfig[idx].children = routerConfig[idx].children.concat(m.children);
  } else {
    routerConfig.push(m);
  }
});
// 设置根路由组件
const idx = routerConfig.findIndex(it => it.path === '/');
if (idx > -1) {
  routerConfig[idx].component = App;
}
export default routerConfig;

三、测试效果

完成代码改造后写了几个页面测试效果,分别是Test,对应代码index.js;SecondPage对应代码SecondPage.js和404error页面。分别输入下面地址即可查看:
http://localhost:9000/#/Test
http://localhost:9000/#/SecondPage
http://localhost:9000/#/404

其代码结构如下:
在这里插入图片描述

四、项目代码

https://github.com/lisiqil/router

欢迎大家下载代码阅读,如有什么错误欢迎指正。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值