高淡可论 發表於 2020-9-14 15:17:00

react 路由组件懒加载

<h1 style="text-align: center">react 路由组件懒加载</h1>
<h2>1 主要依赖插件:</h2>
<p>react-loadable&nbsp; :&nbsp; <span style="background-color: rgba(192, 192, 192, 1)">npm i&nbsp;react-loadable</span></p>
<p>antd&nbsp; :&nbsp; <span style="background-color: rgba(192, 192, 192, 1)">npm i&nbsp;antd</span></p>
<h2>2 配置文件:</h2>
<p>新建 loadable.js</p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">react 版本17》生命周期名称改为 UNSAFE_componentWillMount</span>
import Loadable from 'react-loadable'<span style="color: rgba(0, 0, 0, 1)">;
import { Spin } from </span>'antd'<span style="color: rgba(0, 0, 0, 1)">;
import NotFound from </span>'../components/NotFound/NotFound'<span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(51, 153, 102, 1)">
//默认配置</span>
export const defaultsetting </span>=<span style="color: rgba(0, 0, 0, 1)"> {
timeout: </span>10000<span style="color: rgba(0, 0, 0, 1)">,
delay: </span>300<span style="color: rgba(0, 0, 0, 1)">
};

export const loadable </span>= ( loaders ) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Loadable( {
    loader: loaders.loader,
    delay: loaders.loadering </span>? loaders.loadering.delay ? loaders.loadering.delay : 10000 : 10000<span style="color: rgba(0, 0, 0, 1)">,
    timeout: loaders.loadering </span>? loaders.loadering.timeout ? loaders.loadering.timeout : 300 : 300<span style="color: rgba(0, 0, 0, 1)">,
    loading: Loading,
} );
};

export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> loadable;

</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> Loading ( props ) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> ( props.error ) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;div&gt;
      &lt;NotFound NotFoundmode={ 'Error' }/&gt;
      &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">    );
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> ( props.timedOut ) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;div style=<span style="color: rgba(0, 0, 0, 1)">{ {
      width: </span>'100%'<span style="color: rgba(0, 0, 0, 1)">,
      height: </span>'50rem'<span style="color: rgba(0, 0, 0, 1)">,
      textAlign: </span>'center'<span style="color: rgba(0, 0, 0, 1)">,
      position: </span>'relative'<span style="color: rgba(0, 0, 0, 1)">,
      lineHeight: </span>'50rem'<span style="color: rgba(0, 0, 0, 1)">,
      zIndex: </span>100<span style="color: rgba(0, 0, 0, 1)">,
      } }</span>&gt;
      &lt;Spin tip="请稍等..." size="large"/&gt;
      &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">    );
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> ( props.pastDelay ) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;div style=<span style="color: rgba(0, 0, 0, 1)">{ {
      width: </span>'100%'<span style="color: rgba(0, 0, 0, 1)">,
      height: </span>'50rem'<span style="color: rgba(0, 0, 0, 1)">,
      textAlign: </span>'center'<span style="color: rgba(0, 0, 0, 1)">,
      position: </span>'relative'<span style="color: rgba(0, 0, 0, 1)">,
      lineHeight: </span>'50rem'<span style="color: rgba(0, 0, 0, 1)">,
      zIndex: </span>100<span style="color: rgba(0, 0, 0, 1)">,
      } }</span>&gt;
      &lt;Spin tip="超时,请刷新..." size="large"/&gt;
      &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">    )
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;&gt;&lt;/&gt;
<span style="color: rgba(0, 0, 0, 1)">    )
}
}</span></pre>
</div>
<p>编辑 路由文件:router.jsx</p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">
import {Router, Route, Switch,Redirect} from </span>'react-router-dom'<span style="color: rgba(0, 0, 0, 1)">
import { createBrowserHistory } from </span>'history'<span style="color: rgba(0, 0, 0, 1)">

import {loadable} from </span>'@/utils/loadable'; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">懒加载模块</span>
import Login from '@/pages/login';<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">普通加载模块</span>
const Main = loadable({loader: () =&gt; import('@/pages/main'<span style="color: rgba(0, 0, 0, 1)">)});<span style="color: rgba(51, 153, 102, 1)">   //引用</span>
const NotFound404 </span>= loadable({loader: () =&gt; import('@/components/NotFound/NotFound'<span style="color: rgba(0, 0, 0, 1)">)});<span style="color: rgba(51, 153, 102, 1)">//引用</span>

const history </span>=<span style="color: rgba(0, 0, 0, 1)"> createBrowserHistory();

class RouteMap extends React.Component {
render () {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;Router history={ history }&gt;
      &lt;Switch &gt;
          &lt;Route path="/" exact component={Login}/&gt;
          &lt;Route path="/login" component={Login}/&gt;
          &lt;Route path="/main" component={Main}/&gt;
          &lt;Route path="/404" component={NotFound404}/&gt;
          &lt;Redirect from="/*" to="/404"/&gt;
      &lt;/Switch&gt;
      &lt;/Router&gt;
<span style="color: rgba(0, 0, 0, 1)">    )
}
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> RouteMap

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">_this.props.history.push('/main')//跳转可后退</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">_this.props.history.replace('/main')//跳转不可后退</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> &lt;Redirectfrom="/*" to="/" /&gt; //重定向</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> &lt;Route path="*" component={NotFound404}/&gt;//默认</span></pre>
</div>
<p>&nbsp;</p>
<p>控制台有这种警告时候是因为react版本语法问题</p>
<p><img src="https://img2020.cnblogs.com/blog/1506521/202009/1506521-20200914151250526-2010045108.png" alt="" width="779" height="322" loading="lazy"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>react-dom.development.js:88 Warning: componentWillMount has been renamed, and is not recommended for use. See https://fb.me/react-unsafe-component-lifecycles for details.</p>
<p>* Move code with side effects to componentDidMount, and set initial state in the constructor.<br>* Rename componentWillMount to UNSAFE_componentWillMount to suppress this warning in non-strict mode. In React 17.x, only the UNSAFE_ name will work. To rename all deprecated lifecycles to their new names, you can run `npx react-codemod rename-unsafe-lifecycles` in your project source folder.</p>
<p>Please update the following components: LoadableComponent</p>
<p>&nbsp;</p>
<p>解决方法:</p>
<p>打开&nbsp;<span style="background-color: rgba(192, 192, 192, 1)"> &nbsp;react-loadable&nbsp;&nbsp;</span>&nbsp; &nbsp;插件启动目录,搜索&nbsp;<span style="background-color: rgba(192, 192, 192, 1)">componentWillMount&nbsp;&nbsp;</span>&nbsp; &nbsp;改为&nbsp;&nbsp;<span style="background-color: rgba(192, 192, 192, 1)">UNSAFE_componentWillMount&nbsp; &nbsp;</span>&nbsp;&nbsp;</p>
<p><img src="https://img2020.cnblogs.com/blog/1506521/202009/1506521-20200914151527512-1688955320.png" alt="" width="906" height="441" loading="lazy"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>问题解决。&nbsp; &nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    转载标明来路-博客园,
联系方式1763907618@qq.com<br><br>
来源:https://www.cnblogs.com/wangyongping/p/13666870.html
頁: [1]
查看完整版本: react 路由组件懒加载