欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

react-loadable路由懒加载

程序员文章站 2024-03-14 20:46:53
...

load.js

import Loadable from 'react-loadable';
import './styles/load.styl'
// 按需加载组件
export default function withLoadable (comp) {
    return Loadable({
        loader:comp,
        loading:(props)=>{
            let msg
            if (props.error) {
                return <p>加载错误,请刷新</p>
            } else if (props.timedOut) {
                return <p>加载超时</p>
            } else if (props.pastDelay) {
                return(
                  <div className='loading-mask'>
                    <div className='container'>
                      <img className='loading-img' src={require('../images/loading.png')} />
                      <p>加载中...</p>
                    </div>
                  </div>
                )
            } else {
                return null;
            }
        },
        timeout:10000
    })
}

 

index.js

import Loadable from './assets/load';

const Indexpages = Loadable(()=>import('./pages/Indexpages/index'))