我'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'但是我得到了这个家伙得到的错误:https://github.com/ReactTraining/react-router/issues/1799
当我做localhost:8000时,该应用程序工作 . 当我转到localhost:8000/123时,浏览器呈现 index.html as is with no react. Is Express干扰React?在快递我所拥有的是以下内容:
app.get('*', function response(req, res) {
res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
res.end();
});
这是我的 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));
我的 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>
);
我的 Home 组件在不同的文件中定义如下(省略所有导入和生命周期函数):
export default React.createClass({
render: function() {
{* bunch of JSX goes here *}
}
});
根据要求,我的 index.html 文件:
<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>
由于我的 webpack config (捆绑注入!),html没有捆绑包作为脚本标记(以下代码只是配置的一部分):
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'
})
1 回答
当您向应用添加新路线时,您无法依靠热重新加载来注入新路线组件 . 我不得不按CTRL C再次运行
npm start
.这解决了这个问题!!
react-router-dom也是React Router v4包,而react-router是旧版本 . 感谢@HalCarleton指出这一点 . 我看到代码示例到处都有react-router,而官方文档显示react-router-dom,我真的很困惑为什么有两种不同类型的包 .