0

我对 React Router 还很陌生。我正在使用 React-router-dom 5.0.0

由于某种原因,我的仪表板路线不匹配,当我导航到/dashboard


import React, { Suspense } from 'react';
import ReactDOM from 'react-dom';

import { MuiThemeProvider } from '@material-ui/core/styles';
import theme from './theme';

import { BrowserRouter, Switch, Route, Redirect } from 'react-router-dom';

const MarketPlace = React.lazy(() => import('./marketplace/MarketPlace'));
const Dashboard = React.lazy(() => import('./dashboard/Dashboard'));

const mainApp = (
  <MuiThemeProvider theme={theme}>
    <BrowserRouter>
      <Switch>
        <Redirect exact from="/" to="/marketplace" />
        <Suspense fallback="Loading...">
          <Route component={MarketPlace} exact path="/marketplace" />
        </Suspense>
        <Suspense fallback="Loading...">
          <Route component={Dashboard} exact path="/dashboard" />
        </Suspense>
      </Switch>
    </BrowserRouter>
  </MuiThemeProvider>
);

ReactDOM.render(mainApp, document.getElementById('root'));

我做错了什么吗?

4

1 回答 1

1

Suspense元素应该在您的 switch 块之外,如下所示:

const App: React.FC = (props) => (
  <BrowserRouter>
      <React.Suspense fallback="Loading...">
        <Switch>
          <Route path="/dashboard" component={Dashboard} />
          <Route path="/other" component={Other} />
          <Redirect to="/dashboard" />
        </Switch>
      </React.Suspense>
    </BrowserRouter>
);

例子

于 2019-06-01T07:27:04.337 回答