在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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章