react路由懒加,项目经常上线导致没有刷新的用户出现js文件加载失败: ChunkLoadError: Loading chunk 42 failed.

网友投稿 1263 2022-09-10

react路由懒加,项目经常上线导致没有刷新的用户出现js文件加载失败: ChunkLoadError: Loading chunk 42 failed.

react路由懒加,项目经常上线导致没有刷新的用户出现js文件加载失败: ChunkLoadError: Loading chunk 42 failed.

解决方案:

使用错误边界:

import React, { Component } from 'react'//错误边界//default class ErrorBoundary extends Component { constructor(props) { super(props); this.state = { error: null, errorInfo: null }; } componentDidCatch(error, errorInfo) { // Catch errors in any components below and re-render with error message this.setState({ error: error, errorInfo: errorInfo }) // You can also log error messages to an error reporting service here } render() { if (this.state.errorInfo) { // Error path return (

网页出错了,请尝试刷新一下~
{this.state.error && this.state.error.toString()}
{this.state.errorInfo.componentStack}
); } // Normally, just render children return this.props.children; } }

在路由处使用错误边界:

import React, { Suspense, lazy } from 'react'import { connect } from 'react-redux'import { Switch, Route, Redirect, useHistory, withRouter,} from 'react-router-dom'//自己开发的公共组件会再此处全部引入import { ErrorBoundary, Loading } from '../components/light'const Login = lazy(() => import('../views/light/login/Login'))const SaleLogin = lazy(() => import('../views/sale/login/Login'))const EduLogin = lazy(() => import('../views/edu/login/Login'))const ProducerLogin = lazy(() => import('../views/producer/login/Login'))const Index = lazy(() => import('../views/light/index/Index'))const NotFound = lazy(() => import('../views/light/notFound/NotFound'))function Router({ extraRouter }) { window.reactRouter = useHistory() return ( <> }> {extraRouter.toJS().map((item) => ( ))} )}const mapStateToProps = (state) => { return { extraRouter: state.getIn(['light', 'extraRouter']), }}const mapDispatchToProps = (dispatch) => { return { onSetState(key, value) { dispatch({ type: 'SET_LIGHT_STATE', key, value }) }, onDispatch(action) { dispatch(action) }, }}export default connect(mapStateToProps, mapDispatchToProps)(withRouter(Router))

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Windows WDS和DHCP环境在UEFI平台同时引导Win和Linux
下一篇:构建者(建造者)模式(构造器模式和建造者模式)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~