Reactjs React路由器URL参数不工作
我无法让React Router中的URL参数功能正常工作,任何细节都会有帮助!我尝试使用“react router dom”而不是“react router”,但我发现这家伙犯了以下错误: 当我使用localhost:8000时,该应用程序可以正常工作。当我转到localhost:8000/123时,浏览器会将index.html呈现为不带react的状态。Express是否会干扰react?简而言之,我所拥有的一切如下:Reactjs React路由器URL参数不工作,reactjs,react-router,react-router-v4,react-router-dom,Reactjs,React Router,React Router V4,React Router Dom,我无法让React Router中的URL参数功能正常工作,任何细节都会有帮助!我尝试使用“react router dom”而不是“react router”,但我发现这家伙犯了以下错误: 当我使用localhost:8000时,该应用程序可以正常工作。当我转到localhost:8000/123时,浏览器会将index.html呈现为不带react的状态。Express是否会干扰react?简而言之,我所拥有的一切如下: app.get('*', function response(req,
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));
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>
);
根据要求,我的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>
当您向应用程序添加新路由时,您不能依靠热重新加载来注入新路由+组件。我必须按住CTRL+C键并再次运行
npm start
这就解决了问题
此外,react router dom是react router v4包,而react router是旧版本。感谢@HalCarleton指出这一点。当官方文档显示react router dom时,我到处都能看到react router的代码示例,我真的很困惑,为什么会有两种不同类型的包。尝试这样做:
<Switch>
<Route path="/auth/login/:token" render={props => <Login {...this.props} {...props}/>}/>
<Route path="/auth/login" component={Login}/>
</Switch>
}/>
首先是带参数的路由,然后是不带参数的路由。
在我的登录组件中,我放置了这个
console.log(props.match.params.token)代码>来测试并为我工作。您的问题很可能存在于index.html中,并带有脚本标记以包含您的包。你能帮我把它也发出来吗?@promisified-sure!我已经编辑了我的问题。你能包括你的静态文件服务器设置吗?您的客户端控制台是否有任何错误?另外,您使用的是什么版本的react路由器?您有react-router-v4
和react-router-dom
作为标记,但看起来您使用的是基于所用api的react-router的pre-4版本。@HalCarleton“react-router”:“^2.0.0”,“react-router-dom”:“^4.1.1”。我应该使用“react router dom”吗?我真的很困惑。当我从“react router dom”导入相同的组件时,我会遇到一大堆像此人这样的奇怪错误
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'
})
<Switch>
<Route path="/auth/login/:token" render={props => <Login {...this.props} {...props}/>}/>
<Route path="/auth/login" component={Login}/>
</Switch>