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


    来源: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
    

    很简单把~~




  • 相关阅读:
    SSL证书安装指引
    SSL证书绑定成功
    我是如何将网站全站启用Https的?记录博客安装配置SSL证书全过程
    _INT_SIZEOF 规格严格
    Java: Multidimensional array vs. Onedimensional 规格严格
    Fix JDK TImer 规格严格
    Ecpilse debug视图 规格严格
    PE 分析 规格严格
    VC Unicode 规格严格
    Object Pool 规格严格
  • 原文地址:https://www.cnblogs.com/jiemao/p/9670416.html
Copyright © 2020-2023  润新知