react 路由组件懒加载
<h1 style="text-align: center">react 路由组件懒加载</h1><h2>1 主要依赖插件:</h2>
<p>react-loadable : <span style="background-color: rgba(192, 192, 192, 1)">npm i react-loadable</span></p>
<p>antd : <span style="background-color: rgba(192, 192, 192, 1)">npm i 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 ) =><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><div>
<NotFound NotFoundmode={ 'Error' }/>
</div>
<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><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>>
<Spin tip="请稍等..." size="large"/>
</div>
<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><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>>
<Spin tip="超时,请刷新..." size="large"/>
</div>
<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><></>
<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: () => 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: () => 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><Router history={ history }>
<Switch >
<Route path="/" exact component={Login}/>
<Route path="/login" component={Login}/>
<Route path="/main" component={Main}/>
<Route path="/404" component={NotFound404}/>
<Redirect from="/*" to="/404"/>
</Switch>
</Router>
<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)"> <Redirectfrom="/*" to="/" /> //重定向</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> <Route path="*" component={NotFound404}/>//默认</span></pre>
</div>
<p> </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> </p>
<p> </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> </p>
<p>解决方法:</p>
<p>打开 <span style="background-color: rgba(192, 192, 192, 1)"> react-loadable </span> 插件启动目录,搜索 <span style="background-color: rgba(192, 192, 192, 1)">componentWillMount </span> 改为 <span style="background-color: rgba(192, 192, 192, 1)">UNSAFE_componentWillMount </span> </p>
<p><img src="https://img2020.cnblogs.com/blog/1506521/202009/1506521-20200914151527512-1688955320.png" alt="" width="906" height="441" loading="lazy"></p>
<p> </p>
<p> </p>
<p>问题解决。 </p>
</div>
<div id="MySignature" role="contentinfo">
转载标明来路-博客园,
联系方式1763907618@qq.com<br><br>
来源:https://www.cnblogs.com/wangyongping/p/13666870.html
頁:
[1]