我有一个运行中的React应用程序,我想使用React.lazy实现基于路由的代码分割。
目前我的代码是这样的:
import { PureComponent, cloneElement, Suspense, lazy } from 'react';
...
export const CartPage = lazy(() => import(/* webpackMode: "lazy", webpackPrefetch: true */ 'Route/CartPage'));
...
<Suspense fallback={ this.renderFallbackPage() }>
<NoMatchHandler>
<Switch>
...
<Route path="/cart" exact component={ CartPage } />
...
</Switch>
</NoMatchHandler>
</Suspense>
这里只列出相关部分以使其更紧凑。
现在的问题是,在webpack-dev-server中运行得非常完美,但当我运行npm run build并转到/cart
时,代码就会崩溃。按照链接中提到的,这是错误信息:
Element type is invalid. Received a promise that resolves to: function i(e){var r;return
r=t.call(this,e)||this,T()(y?!e.wrapperProps[d]:!e[d],"Passing redux store in props has
been removed and does not do anything.
"+P),r.selectDerivedProps=n(),r.selectChildElement=function(){var t,e,n,r;return
function(i,o,a){return(o!==t||a!==e||r!==i)&&(t=o,e=a,r=i,n=m.a.createElement(i,Object(O.a)
({},o,{ref:a}))),n}}
(),r.indirectRenderWrappedComponent=r.indirectRenderWrappedComponent.bind(function(t)
{if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been
called");return t}(r)),r}. Lazy element type must resolve to a class or function.
我已经完成了一些常见的故障排除:
- 在
CartPage
组件中,我已经执行了export default connect(mapStateToProps, mapDispatchToProps)(CartPage);
- React版本为
16.13.1
奇怪的是,Received a promise that resolves to: function...
。它是一个函数!但是它却抱怨Lazy element type must resolve to a class or function
。这对我来说毫无意义。
可能出了什么问题?
编辑
Route/CartPage/index.js
有以下内容:
import { PureComponent } from 'react';
export default class CartPage extends PureComponent {
render() {
return <h1>Test</h1>;
}
}
我特意尽可能地让它变得简单。但是还是出现了相同的错误。但是参数不同。现在的错误是 this
Element type is invalid. Received a promise that resolves to: function
t(){return c()(this,t),r.apply(this,arguments)}. Lazy element type
must resolve to a class or function.
编辑2
我从我的webpack.config.js
中删除了以下行。然后它开始工作!但还是不知道为什么。
const MinifyPlugin = require('babel-minify-webpack-plugin');
...
plugins: [
...,
new MinifyPlugin({
removeConsole: false,
removeDebugger: false
}, {
comments: false
})
]
...
默认导出(default export)
。 - nickbullock