使用 dva 如何配置异步加载路由组件

2018-01-09  本文已影响0人  静夜秋雨
如果你觉得该文章对你有帮助加个喜欢,github 加个 start 谢谢

普通方式 文件目录: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

很简单把~~

如果你对文章感兴趣 或者有其他技术问题探讨

qq群:218618405
github 地址:https://github.com/Sawyer-china/

上一篇下一篇

猜你喜欢

热点阅读