来源:https://www.jianshu.com/p/69694013e36b
-----------------------------------------------------
普通方式 文件目录:src/route.js
import React from 'react'
import { Router, Route, Switch } from 'dva/router'
import IndexPage from './routes/IndexPage';
import Category from './routes/Category';
function RouterConfig({ history, app }) {
return (
<Router history={history}>
<Switch>
<Route path="/" exact component={IndexPage} />
<Route path="/category" exact component={Category} />
</Switch>
</Router>
)
}
export default RouterConfig
1. 使用异步路由加载组件 在文件头部引入
import dynamic from 'dva/dynamic'
然后删除以下代码
import IndexPage from './routes/IndexPage';
import Category from './routes/Category';
在RouteConfig函数中添加以下代码即可
const CompontentPage = dynamic({
component: () => import('./routes/CompontentPage')
})
const CompontentPage1 = dynamic({
component: () => import('./routes/CompontentPage1')
})
完整实例
import React from 'react'
import { Router, Route, Switch } from 'dva/router'
import dynamic from 'dva/dynamic'
function RouterConfig({ history, app }) {
const IndexPage = dynamic({
app,
component: () => import('./routes/IndexPage')
})
const Category = dynamic({
app,
component: () => import('./routes/Category')
})
return (
<Router history={history}>
<Switch>
<Route path="/" exact component={IndexPage} />
<Route path="/category" exact component={Category} />
</Switch>
</Router>
)
}
export default RouterConfig
很简单把~~