javascript 异步反应路由器组件加载

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 异步反应路由器组件加载相关的知识,希望对你有一定的参考价值。

import React, { lazy, Suspense } from 'react'
import ReactDOM from 'react-dom'
import { Link, Route, BrowserRouter as Router, Switch } from 'react-router-dom'

const FunComponent = (
  lazy(() => (
    import('./FunComponent')
  ))
)

const TerribleComponent = (
  lazy(() => (
    import('./TerribleComponent')
  ))
)

const TestComponent = (
  lazy(() => (
    import('./TestComponent')
  ))
)

const LoadingMessage = () => (
  "I'm loading..."
)

const App = () => (
  <Router>
    <div>
      <header>
        <nav>
          <div>
            <Link to="/">
              Home
            </Link>
          </div>

          <div>
            <Link to="/fun">
              Fun
            </Link>
          </div>

          <div>
            <Link to="/terrible">
              Terrible
            </Link>
          </div>
        </nav>
      </header>

      <br />

      <Suspense fallback={<LoadingMessage />}>
        <Switch>
          <Route path="/fun">
            <FunComponent />
          </Route>

          <Route path="/terrible">
            <TerribleComponent />
          </Route>

          <Route>
            <TestComponent />
          </Route>
        </Switch>
      </Suspense>
    </div>
  </Router>
)

const rootElement = document.getElementById('root')
ReactDOM.render(<App />, rootElement)

以上是关于javascript 异步反应路由器组件加载的主要内容,如果未能解决你的问题,请参考以下文章

vue 异步组件

vue异步组件

vue 路由懒加载

javascript 复杂异步反应路由器v4示例

javascript 简单的异步反应路由器v4示例

vue路由懒加载