首页 > 解决方案 > 使用 react-router 构建不显示我的应用程序(空白页面)

问题描述

我构建了我的应用程序,但屏幕上没有任何内容。

我使用 react-router 作为我的页面之间的路径,但没有显示任何内容......

我的应用程序需要位于子文件夹中:文件夹/子文件夹/子文件夹作为主链接。

我不知道我错在哪里

这是我的路线:

<Router basename="/">
<div>
    <Route exact path="/" component={App}/>
    <Route path="/scene01" component={Scene01}/>
</div>
</Router>

我的 package.json:

"name": "impermo",
  "version": "0.1.0",
  "private": true,
  "homepage": "/folder/subfolder/subfolder/",
  "dependencies": {
    "@material-ui/core": "^4.9.12",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.4.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }

标签: reactjsbuildreact-router

解决方案


您需要添加一个Switch来自react-router

来自ReactTraining.com

A<Switch>查看它的 children<Route>并呈现与当前 URL 匹配的第一个。

<Router basename="/">
  <Switch>
    <Route exact path="/" component={App}/>
    <Route path="/scene01" component={Scene01}/>
  </Switch>
</Router>

推荐阅读