【发布时间】:2023-04-02 09:35:01
【问题描述】:
我在我的应用程序中使用“react-loadable”来延迟加载一些我不想在应用程序加载后立即呈现的组件,目前我正在遵循基于路由的代码拆分。
我的文件结构:
ignitus-About/Components/index.js的内容如下:
export { default as About } from './About';
这是我的懒加载 AboutUs 组件的代码 sn-p:
const AboutUs = Loadable({
loader: () => import('../ignitus-About/Components/About'),
loading: Loading,
});
但您会注意到,我正在编写 About 组件的确切/完整路径,但在我的 Components 目录中,我只有 2 个文件,一个是 index.js,另一个是 About.js。
这里index.js 正在通过这样做导出关于 组件:
export { default as About } from './About';
但是当我在我的可加载组件中写这个时:
const AboutUs = Loadable({
loader: () => import('../ignitus-About/Components'),
loading: Loading,
});
它会抛出一个错误,所以我的问题是 react-lodable 是否期望组件的确切路径,如果不是,那么我如何从可加载组件中的 index.js 导出我的 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)
【问题讨论】:
-
你能详细说明你的代码是如何“抛出和错误”的吗?你期待什么,实际发生了什么?如果您遇到异常/错误,请发布它发生的行和异常/错误详细信息。请edit这些详细信息,否则我们可能无法提供帮助。
-
@FrankerZ 我已经用错误和更多细节更新了问题。
标签: reactjs react-router lazy-loading react-loadable