反应路由器 URL 参数不起作用 [英] React Router URL parameters not working
问题描述
我无法使 React Router 中的 URL 参数功能正常工作,任何见解都会有所帮助!我尝试使用 'react-router-dom' 而不是 'react-router' 但我得到了这个人得到的错误:https://github.com/ReactTraining/react-router/issues/1799
当我执行 localhost:8000 时,应用程序可以工作.当我转到 localhost:8000/123 时,浏览器会按原样呈现 index.html 没有反应. Express 是否会干扰 React?在 Express 中,我只有以下内容:
app.get('*', function response(req, res) {res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));重发();});
这是我的 main.js:
import 'babel-polyfill';从反应"导入反应;从 'react-dom' 导入 ReactDOM;从反应路由器"导入{路由器,浏览器历史};从./shared/components/Routes"导入路由;const ROOT_ELEMENT = 'app';ReactDOM.render((<路由器历史={browserHistory}>{路线}</路由器>), document.getElementById(ROOT_ELEMENT));
我的routes.js:
import { Route, IndexRoute, Link } from 'react-router';从'./App'导入应用程序;从../../pages/home/page"导入主页;从../../pages/about/page"导入关于页面;const Child = ({ match }) =>(<div><h3>ID:{match.params.id}</h3>
)导出默认值 (<Route path="/" component={App}><IndexRoute 组件={HomePage}/>{/* <Route path="about" component={AboutPage}/>*/}<Route path="/:id" component={Child}/></路线>);
我的 Home 组件在不同的文件中是这样定义的(忽略了所有的导入和生命周期函数):
导出默认 React.createClass({渲染:函数(){{* 一堆 JSX 放在这里 *}}});
根据要求,我的 index.html 文件:
<头><meta charset="utf-8"><meta http-equiv="x-ua-compatible" content="ie=edge"><title>同意书</title><meta name="description" content="隐私同意网络表单"><meta name="viewport" content="width=device-width, initial-scale=1"><!--Google Material Design 图标--><link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">头部><身体><div id="应用程序"></div></html>
由于我的 webpack 配置,html 没有将包作为脚本标记(包被注入!)(以下代码只是配置的一部分):
条目:['webpack-hot-middleware/client','引导加载程序',path.join(__dirname, 'src/main.js')],输出: {路径:path.join(__dirname, '/dist/'),文件名:'[名称].js',公共路径:'/'},插件: [新的 HtmlWebpackPlugin({模板:'src/index.tpl.html',注入:'身体',文件名:'index.html'})
当你向你的应用添加新的路由时,你不能依赖热重载来注入新的路由 + 组件.我不得不 CTRL+C 并再次运行 npm start
.
这解决了问题!!
还有 react-router-dom 是 React Router v4 包,而 react-router 是旧版本.感谢@HalCarleton 指出这一点.我看到到处都是 react-router 的代码示例,而官方文档显示的是 react-router-dom,我真的很困惑为什么会有两种不同类型的包.
I'm unable to get the URL parameter feature in React Router to work, any insight would be helpful! I tried using the 'react-router-dom' instead of 'react-router' but I get the errors this guy is getting: https://github.com/ReactTraining/react-router/issues/1799
When I do localhost:8000, the app works. When I go to localhost:8000/123, the browser renders the index.html as is with no react. Is Express interfering with React? In Express all i have is the following:
app.get('*', function response(req, res) {
res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
res.end();
});
Here's my main.js:
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, browserHistory } from 'react-router';
import Routes from './shared/components/Routes';
const ROOT_ELEMENT = 'app';
ReactDOM.render((
<Router history={browserHistory}>
{Routes}
</Router>
), document.getElementById(ROOT_ELEMENT));
My routes.js:
import { Route, IndexRoute, Link } from 'react-router';
import App from './App';
import HomePage from '../../pages/home/page';
import AboutPage from '../../pages/about/page';
const Child = ({ match }) => (
<div>
<h3>ID: {match.params.id}</h3>
</div>
)
export default (
<Route path="/" component={App}>
<IndexRoute component={HomePage} />
{/* <Route path="about" component={AboutPage} /> */}
<Route path="/:id" component={Child} />
</Route>
);
My Home component is defined like this in a different file (left out all the imports and lifecycle functions):
export default React.createClass({
render: function() {
{* bunch of JSX goes here *}
}
});
As requested, my index.html file:
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Consent Form</title>
<meta name="description" content="Privacy Consent Webform">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--Google Material Design Icons-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
<div id="app"></div>
</body>
</html>
The html doesn't have the bundle as a script tag because of my webpack config (the bundle is injected!)(the following code is just a piece of the config):
entry: [
'webpack-hot-middleware/client',
'bootstrap-loader',
path.join(__dirname, 'src/main.js')
],
output: {
path: path.join(__dirname, '/dist/'),
filename: '[name].js',
publicPath: '/'
},
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.tpl.html',
inject: 'body',
filename: 'index.html'
})
When you add new routes to your app, you can't rely on hot reloading to inject the new route + components. I had to CTRL+C and run npm start
again.
This solves the issue!!
Also react-router-dom is a React Router v4 package while react-router is an older version. Thanks to @HalCarleton for pointing that out. I saw code examples everywhere with react-router while the official documentation showed react-router-dom and I was really confused as to why there were two different types of packages.
这篇关于反应路由器 URL 参数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!