在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

 

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