【发布时间】:2019-10-27 18:48:40
【问题描述】:
我在动态路由上使用了 reactlazy 和 suspense,但不知何故我无法渲染延迟加载的组件。
我已经搜索过惰性路由的使用,但我没有看到有人在动态(localhost:8080/dynamic/dynamic)路由上使用它.
在动态路由上加载组件对我有用,如果我有静态路由,延迟加载也可以,但是当我尝试将两者结合时,组件不会加载。
这是我所做的一个例子,
import Loader from './path/toFile';
import Home from './path/toFile';
const LazyLoadedComponent = lazy(() => import('./path/toFile'));
const App = () => {
return(
<Router>
<Switch>
// Home has multiple views controlled by buttons
// A view contains selections with corresponding id
<Route exact path="/:viewType?" component={Home} />
// this component doesn't load when I select something which links me to this dynamic route.
<Suspense fallback={Loader}>
<Route path="/viewType/:selectionId" component={LazyLoadedComponent} />
</Suspense>
</Switch>
</Router>
)
}
我只想在我去那条路线后加载我的组件。 但结果是它加载了 Home 但是当我从选择中选择一个时,它只显示 index.html 和空白 我没有看到任何错误产生。
【问题讨论】:
标签: reactjs react-router lazy-loading react-suspense