[英]Why doesn't my react-routing work after webpack building?
我做了一個簡單的反應路由器。 npm start
開發時,我使用的是npm start
命令,該命令啟動了開發服務器。 而且一切正常。 但是,當我使用命令npm run build
(使用webpack) npm run build
應用程序,啟動簡單的http服務器並轉到/ add_project時,出現404錯誤。 而且我不知道為什么會這樣。 請幫助!
這是代碼:
App.js
imports...
render() {
return (
<Switch>
<div>
<Route exact path='/' component={Projects}/>
<Route exact path='/add_project' component={AddProject} />
</div>
</Switch>
);
}}
index.js
imports...
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById('root')
);
webpack.config.js
const HtmlPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src',
output: {
filename: 'app.js',
path: __dirname + '/dist'
},
devtool: 'source-map',
module: {
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}]
},
plugins: [
new HtmlPlugin({
template: 'public/index.html'
})
]
}
UPD:
package.json
{
"name": "Test",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"build": "webpack -p --progress --config webpack.config.js",
"dev-build": "webpack --progress -d --config webpack.config.js",
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack --progress -d --config webpack.config.js --watch",
"start": "react-scripts start"
},
"license": "MIT",
"babel": {
"presets": [
"es2015",
"react"
]
},
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"bootstrap": "^4.0.0",
"css-loader": "^0.28.4",
"eslint": "^4.18.1",
"eslint-plugin-react": "^7.7.0",
"extract-text-webpack-plugin": "^2.1.2",
"file-loader": "^0.11.2",
"jquery": "^3.2.1",
"react": "^15.6.1",
"react-bootstrap": "^0.31.0",
"react-dom": "^15.6.1",
"react-router-dom": "^4.2.2",
"style-loader": "^0.18.2",
"webpack": "^3.11.0"
}
}
UPD2:
AddProject.js
import React, { Component } from 'react';
class AddProject extends Component {
constructor()
{
super();
}
render() {
return (
<form className="form-horizontal">
<input type="text" ref="title" className="input" />
<input type="text" ref=""/>
</form>
);
}
}
export default AddProject;
您已經將精確路徑與'/'匹配,然后再次將其與/ add_project路由匹配。 嘗試改變
<Route exact path='/add_project' component=
{AddProject} />
至
<Route path='/add_project' component=
{AddProject} />
如果在父路徑上設置了精確,則子路徑不匹配,但按預期方式顯示404
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.