react-router-dom BrowserRouter 不导航到具有多个 url 参数的路径

react-router-dom BrowserRouter not navigating to paths with more than one url parameter

我遇到一个问题,在使用 react-router-dom@4.2.2 时,我无法导航到路径大于 1 个部分的路线:此外,这是 Github link 到我遇到这个问题的仓库。

https://github.com/ShawnCholeva/React-Typescript-Webpack4-Router

import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
//All routes work if I use HashRouter here instead of BrowserRouter

class App extends Component {
    render() {
        return (
            <Router>
                <Switch>
                    <Route exact path='/' component={ Login }/> <--- Works
                    <Route exact path='/accountSetup/:guid?' component={ AccountSetup }/> 
                    ^-- Above works when just '/accountSetup' but fails when I do '/accountSetup/123'
                    <Route component={ NotFound } /> <--- Works
                </Switch>
            </Router>
        );
    }
}

所以我注意到这仅在我使用 HashRouter 而不是 BrowserRouter 时才有效。所以这让我想到这可能是我通过 webpack-dev-server@3.1.1 为应用程序提供服务的方式。所以我看到我必须使用 webpack@4.2.0 将 historyApiFallback 属性 添加到我的 devServer 属性对象中,如下所示:

devServer: {
    compress: true,
    port: 8080,
    historyApiFallback: true,
    contentBase: path.resolve(__dirname),
    publicPath: '/',
}

然后我 运行 通过我 package.json 中的 npm 脚本应用程序,如下所示:

"start": "webpack-dev-server --open --mode development"

所以当我导航到 localhost:5000/accountSetup/123 时,我在控制台中收到以下错误:

我也曾尝试使用 http-server-spa 包提供此服务,并在导航时注意到以下日志:

[OK] Serving static files from ./dist
[OK] Using the fallback file index.html
[OK] Listening on http://localhost:8080
----------------------------------------------
[OK] GET /accountSetup
[OK] GET /bundle.js
[OK] GET /accountSetup/123
[ER] GET /accountSetup/bundle.js

好像是把最后一个路由参数url中的前缀添加到路由加载bundle.js中。所以你可以看到,当我导航到 accountSetup 时,它只是引用了 bundle.js,因为它是 url 中的最后一段。但是当我添加多个段时,它似乎将先前的段从 localhost 附加到最后一个段,到 bundle.js

的路径

编辑:

似乎与使用动态路由的 BrowserRouter 和使用静态路由的 HashRouter 有关。

这是我的完整 package.json 和 webpack.config.js 文件

package.json:

{
  "name": "react-webpack4-typescript-skeleton",
  "version": "1.0.0",
  "description": "React application using Webpack 4 and Typescript",
  "main": "index.tsx",
  "scripts": {
    "start": "webpack-dev-server --open --mode development",
    "prestart": "npm run test",
    "build:prod": "webpack --mode production --config webpack.config.js",
    "lint": "tslint 'src/**/*.{ts,tsx}'",
    "test": "jest",
    "pretest": "npm run lint",
    "build:docker": "docker-compose up --build -d"
  },
  "keywords": [
    "React",
    "Webpack 4",
    "Typescript"
  ],
  "author": "Shawn Choleva",
  "license": "MIT",
  "devDependencies": {
    "@types/jest": "^22.2.2",
    "@types/react": "^16.0.41",
    "@types/react-dom": "^16.0.4",
    "@types/react-redux": "^5.0.15",
    "@types/react-router-dom": "^4.2.5",
    "@types/redux": "^3.6.0",
    "@types/webpack": "^4.1.2",
    "@types/webpack-dev-server": "^2.9.4",
    "css-loader": "^0.28.11",
    "file-loader": "^1.1.11",
    "html-webpack-plugin": "^3.1.0",
    "jest": "^22.4.3",
    "less-loader": "^4.1.0",
    "style-loader": "^0.20.3",
    "ts-jest": "^22.4.2",
    "ts-loader": "^4.1.0",
    "tslint": "^5.9.1",
    "tslint-react": "^3.5.1",
    "typescript": "^2.7.2",
    "url-loader": "^1.0.1",
    "webpack": "^4.2.0",
    "webpack-cli": "^2.0.13",
    "webpack-dev-server": "^3.1.1"
  },
  "dependencies": {
    "less": "^3.0.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-redux": "^5.0.7",
    "react-router-dom": "^4.2.2",
    "redux": "^3.7.2"
  },
  "jest": {
    "transform": {
      "^.+\.tsx?$": "ts-jest"
    },
    "testRegex": "(/tests/.*|(\.|/)(test|spec))\.(jsx?|tsx?)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  }
}

webpack.config.js:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin')

const config = {
  entry: './src/index.tsx',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      },
      {
        test: /\.less$/,
        use: [
          {
            loader: "style-loader"
          },
          {
            loader: "css-loader",
            options: {
              sourceMap: true,
              modules: true,
              localIdentName: '[local]___[hash:base64:5]'
             }
          },
          {
            loader: "less-loader"
          }
        ]
      },
      { 
        test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        use:'url-loader' 
      },
      { 
        test: /\.(png|jp(e*)g|svg)$/,
        use: [
          {
            loader: "url-loader",
            options: {
              limit: 10000,
              name: 'images/[hash]-[name].[ext]',
            },
          }
        ]
      }
    ]
  },
  resolve: {
    extensions: [ '.ts', '.tsx', '.js' ]
  },
  plugins: [
    new HtmlWebpackPlugin({
        title: 'Application Name',
        template: path.join(__dirname, 'src/index.html')
    })
  ],
  devtool: 'inline-source-map',
  devServer: {
    compress: true,
    port: 8080,
    historyApiFallback: true,
    contentBase: path.resolve(__dirname),
    publicPath: '/',
  }
};

module.exports = config;

您似乎在尝试创建嵌套路由。但是在 ReactRouter v4 中,正如您指出的那样使用动态路由,嵌套路由应该嵌套在组件内部。在你的AccountSetup渲染方法中你应该添加的意思。

render(){
 //...
  <Route
    path={match.url + '/test'}
    component={Test}
  />
// ...
}

docs. Here is a live example 中阅读有关嵌套路由的更多信息。
如果你确实想使用静态路由,你可以 try this.

我想我已经找到问题所在了。您的 webpack 配置中缺少 publicPath。这是更新后的输出配置:

output: {
   filename: 'bundle.js',
   path: path.resolve(__dirname, 'dist'),
   publicPath: '/' <-- ADDED THIS
  },