16.13.0 中的 React 延迟加载问题
React Lazy Loading Issue in 16.13.0
import React, { Component,lazy,Suspense} from 'react';
import LayoutComponent from './components/layout/Layout'
import BurgerBuilderContainer from './Containers/BurgerBuilder/BurgerBuilder'
import CheckoutContainer from './Containers/CheckOut/Checkout'
import { Route, Switch,withRouter } from 'react-router-dom'
const OrdersContainer = lazy(()=> {
return import('./Containers/Orders/Orders')
})
render(){
return (
<div>
<Suspense fallback={<div>Loading...</div>}>
<LayoutComponent>
<Switch>
<Route path="/Logout" component={LogoutContainer}></Route>
<Route path="/Auth" component={AuthContainer}></Route>
<Route path="/Orders" component={OrdersContainer}></Route>
<Route path="/checkout" component={CheckoutContainer}></Route>
<Route path="/" component={BurgerBuilderContainer}></Route>
</Switch>
</LayoutComponent>
</Suspense>
</div>
);
}
根据错误延迟加载显示导入错误,在使用延迟加载应用程序之前工作正常。
你可以试试
const OrdersContainer= React.lazy(() => import('./Containers/Orders/Orders'));
如果错误继续尝试
const OrdersContainer= React.lazy(() => require('./Containers/Orders/Orders'));
如果还在继续
React.lazy(() => import("./Containers/Orders/Orders").then(x => x.default))
最后
进口@babel/plugin-syntax-dynamic-import
import React, { Component,lazy,Suspense} from 'react';
import LayoutComponent from './components/layout/Layout'
import BurgerBuilderContainer from './Containers/BurgerBuilder/BurgerBuilder'
import CheckoutContainer from './Containers/CheckOut/Checkout'
import { Route, Switch,withRouter } from 'react-router-dom'
const OrdersContainer = lazy(()=> {
return import('./Containers/Orders/Orders')
})
render(){
return (
<div>
<Suspense fallback={<div>Loading...</div>}>
<LayoutComponent>
<Switch>
<Route path="/Logout" component={LogoutContainer}></Route>
<Route path="/Auth" component={AuthContainer}></Route>
<Route path="/Orders" component={OrdersContainer}></Route>
<Route path="/checkout" component={CheckoutContainer}></Route>
<Route path="/" component={BurgerBuilderContainer}></Route>
</Switch>
</LayoutComponent>
</Suspense>
</div>
);
}
根据错误延迟加载显示导入错误,在使用延迟加载应用程序之前工作正常。
你可以试试
const OrdersContainer= React.lazy(() => import('./Containers/Orders/Orders'));
如果错误继续尝试
const OrdersContainer= React.lazy(() => require('./Containers/Orders/Orders'));
如果还在继续
React.lazy(() => import("./Containers/Orders/Orders").then(x => x.default))
最后
进口@babel/plugin-syntax-dynamic-import