我在我的应用程序中使用“react-loadable”来延迟加载一些我不想在应用程序加载后立即渲染的组件,目前我正在遵循基于路由的代码拆分。
我的文件结构:
内容ignitus-About/Components/index.js
如下:
export { default as About } from './About';
这是我的延迟加载 AboutUs 组件的代码片段:
const AboutUs = Loadable({
loader: () => import('../ignitus-About/Components/About'),
loading: Loading,
});
但是您会在这里注意到的是,我正在编写 About 组件的确切/完整路径,但在我的Components
目录中,我只有 2 个文件一个index.js
和另一个About.js
。
这index.js
是通过执行此操作导出About组件:
export { default as About } from './About';
但是当我在我的可加载组件中写这个时:
const AboutUs = Loadable({
loader: () => import('../ignitus-About/Components'),
loading: Loading,
});
index.js
它会引发错误,所以我的问题是 react-lodable 是否期望组件的确切路径,如果不是,那么如何从可加载组件中导出我的 About组件。
所以,当我像这样延迟加载我的组件时:
const AboutUs = Loadable({
loader: () => import('../ignitus-About/Components/About'),
loading: Loading,
});
它工作正常。
如果我尝试像这样延迟加载:
const AboutUs = Loadable({
loader: () => import('../ignitus-About/Components'),
loading: Loading,
});
它抛出一个错误:
index.js:1452 Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Check the render method of `LoadableComponent`.
in LoadableComponent (created by Route)
in Route (at publicRoutes.js:56)
in Switch (at publicRoutes.js:41)
in div (at publicRoutes.js:39)
in PublicRoutes (created by Route)
in Route (at App.js:36)
in Switch (at App.js:34)
in div (at App.js:25)
in App (at src/index.js:29)
in Router (created by BrowserRouter)
in BrowserRouter (at src/index.js:28)
in Provider (at src/index.js:27)