在react中使用类vue的路由

  1. 项目目录
  2. eslint规范
  3. 路由优化
    1. /src/router.js 类vue的方式定义路由,dynamic做路由懒加载
import { Router, Route, Switch } from 'dva/router';
import dynamic from 'dva/dynamic' // 路由和models懒加载,按需加载
import routes from './routes' // 路由的进一步抽离,抽离之后这个文件就不在需要更改了
function RouterConfig({ history, app }) {
  return (
    <Router history={history}>
      <Switch>
        {
          routes.map((item, key) => {
            return <Route key={key} path={item.path} exact={item.exact} component={dynamic({
              app,
              component: item.component,
              models: item.models
            })} />
          })
        }
      </Switch>
    </Router>
  )
}
export default RouterConfig
    1. 同时也有./routes的代码
const routes = [
  {
    path: '/', // 首页
    exact: true,
    component: () => import('./home/index.js'),
    models: () => [import('../models/home')]
  }
]
export default routes
发布了24 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43869192/article/details/103970305