为什么我的 React 路由器只加载默认路由?
Why my React router only loads the default route?
我正在使用 react-router 学习 React 应用程序中的页面导航,
这是我的package.json
{
"name": "tutorial",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --inline --content-base .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Rahul Shivsharan",
"license": "ISC",
"dependencies": {
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.3.4",
"@babel/plugin-proposal-class-properties": "^7.3.4",
"@babel/preset-env": "^7.3.4",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5",
"http-server": "^0.11.1",
"webpack": "^4.29.6",
"webpack-cli": "^3.2.3",
"webpack-dev-server": "^3.2.1"
}
}
这是我的App.js
import React from "react";
import {Link} from "react-router";
export default class App extends React.Component{
render(){
return <div>Hello, React Router !!</div>
}
}
这是Repos.js
import React from "react"
export default class Repos extends React.Component{
render(){
return <div>Repos rendered !!!</div>
}
}
这是About.js
import React from "react"
export default class About extends React.Component{
render(){
return <div>About to be !!</div>
}
}
这是index.js
import React from "react";
import ReactDOM from "react-dom";
import {BrowserRouter,Route, Switch} from "react-router-dom";
import App from "./modules/App";
import Repos from "./modules/Repos";
import About from "./modules/About";
ReactDOM.render(
(<BrowserRouter>
<Switch>
<Route path="/" component={App} />
<Route path="/repos" component={Repos} />
<Route path="/about" component={About} />
</Switch>
</BrowserRouter>), document.getElementById("app"));
这是我的index.html
<!DOCTYPE html public "storage">
<html>
<head>
<meta charset="UTF-8">
<title>My First React Router App</title>
</head>
<body>
<div id="app"></div>
<script src="./bundle.js"></script>
</body>
</html>
这是我的webpack.config.js
module.exports = {
"entry" : "./index.js",
"output" : {
"filename" : "bundle.js",
"publicPath" : ""
},
"module" : {
"rules" : [{
"test" : /\.js$/,
"exclude" : /node_modules/,
"loader" : "babel-loader",
"query": {
"presets": ['@babel/react', '@babel/preset-env'],
"plugins": ['@babel/proposal-class-properties']
}
}]
}
}
我的问题是当我使用
启动服务器时
npm start
在浏览器中加载
你好,反应路由器!!
当我做的时候
http://localhost:8081/about
这表明,
无法获取 /about
正在加载
无法获取 /repos
为什么我无法加载默认路径以外的路径?
请帮帮我,我是不是做错了什么,
您需要启用 historyApiFallback
以便 webpack-dev-server 将为当前提供 404 Not Found
响应的所有路由提供 index.html
文件。
webpack.config.js
module.exports = {
entry: "./index.js",
output: {
filename: "bundle.js",
publicPath: ""
},
devServer: {
historyApiFallback: true
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader",
query: {
presets: ["@babel/react", "@babel/preset-env"],
plugins: ["@babel/proposal-class-properties"]
}
}
]
}
};
我能够解决我的问题,
我只需要在 index.js 中做一些更改,如下所示,
import React from "react";
import ReactDOM from "react-dom";
import {BrowserRouter,Route, Switch} from "react-router-dom";
import App from "./modules/App";
import Repos from "./modules/Repos";
import About from "./modules/About";
ReactDOM.render(
(<BrowserRouter>
<Switch>
<Route exact path="/" component={App} />
<Route exact path="/repos" component={Repos} />
<Route exact path="/about" component={About} />
</Switch>
</BrowserRouter>), document.getElementById("app"));
上面你可以看到我在Route标签
中添加了单词"exact"
其次,根据上面 Thole 的回答所建议的,我在 webpack.config.js 中手动添加了一个额外的配置,即如下,
"devServer": {
"historyApiFallback": true
}
就是这样,现在上面的例子可以了,
所以现在,
我加载 html 在 App.js
中返回
它加载 html 在 About.js 中返回,依此类推
我正在使用 react-router 学习 React 应用程序中的页面导航,
这是我的package.json
{
"name": "tutorial",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --inline --content-base .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Rahul Shivsharan",
"license": "ISC",
"dependencies": {
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.3.4",
"@babel/plugin-proposal-class-properties": "^7.3.4",
"@babel/preset-env": "^7.3.4",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5",
"http-server": "^0.11.1",
"webpack": "^4.29.6",
"webpack-cli": "^3.2.3",
"webpack-dev-server": "^3.2.1"
}
}
这是我的App.js
import React from "react";
import {Link} from "react-router";
export default class App extends React.Component{
render(){
return <div>Hello, React Router !!</div>
}
}
这是Repos.js
import React from "react"
export default class Repos extends React.Component{
render(){
return <div>Repos rendered !!!</div>
}
}
这是About.js
import React from "react"
export default class About extends React.Component{
render(){
return <div>About to be !!</div>
}
}
这是index.js
import React from "react";
import ReactDOM from "react-dom";
import {BrowserRouter,Route, Switch} from "react-router-dom";
import App from "./modules/App";
import Repos from "./modules/Repos";
import About from "./modules/About";
ReactDOM.render(
(<BrowserRouter>
<Switch>
<Route path="/" component={App} />
<Route path="/repos" component={Repos} />
<Route path="/about" component={About} />
</Switch>
</BrowserRouter>), document.getElementById("app"));
这是我的index.html
<!DOCTYPE html public "storage">
<html>
<head>
<meta charset="UTF-8">
<title>My First React Router App</title>
</head>
<body>
<div id="app"></div>
<script src="./bundle.js"></script>
</body>
</html>
这是我的webpack.config.js
module.exports = {
"entry" : "./index.js",
"output" : {
"filename" : "bundle.js",
"publicPath" : ""
},
"module" : {
"rules" : [{
"test" : /\.js$/,
"exclude" : /node_modules/,
"loader" : "babel-loader",
"query": {
"presets": ['@babel/react', '@babel/preset-env'],
"plugins": ['@babel/proposal-class-properties']
}
}]
}
}
我的问题是当我使用
启动服务器时npm start
在浏览器中加载 你好,反应路由器!!
当我做的时候 http://localhost:8081/about
这表明, 无法获取 /about
正在加载 无法获取 /repos
为什么我无法加载默认路径以外的路径?
请帮帮我,我是不是做错了什么,
您需要启用 historyApiFallback
以便 webpack-dev-server 将为当前提供 404 Not Found
响应的所有路由提供 index.html
文件。
webpack.config.js
module.exports = {
entry: "./index.js",
output: {
filename: "bundle.js",
publicPath: ""
},
devServer: {
historyApiFallback: true
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader",
query: {
presets: ["@babel/react", "@babel/preset-env"],
plugins: ["@babel/proposal-class-properties"]
}
}
]
}
};
我能够解决我的问题,
我只需要在 index.js 中做一些更改,如下所示,
import React from "react";
import ReactDOM from "react-dom";
import {BrowserRouter,Route, Switch} from "react-router-dom";
import App from "./modules/App";
import Repos from "./modules/Repos";
import About from "./modules/About";
ReactDOM.render(
(<BrowserRouter>
<Switch>
<Route exact path="/" component={App} />
<Route exact path="/repos" component={Repos} />
<Route exact path="/about" component={About} />
</Switch>
</BrowserRouter>), document.getElementById("app"));
上面你可以看到我在Route标签
中添加了单词"exact"其次,根据上面 Thole 的回答所建议的,我在 webpack.config.js 中手动添加了一个额外的配置,即如下,
"devServer": {
"historyApiFallback": true
}
就是这样,现在上面的例子可以了,
所以现在,
我加载 html 在 App.js
中返回它加载 html 在 About.js 中返回,依此类推