如何正确设置反应路由器v3?

时间:2018-02-07 12:32:32

标签: reactjs react-router

我试图为我的反应项目设置react-router但由于某种原因无法正确设置路径。在路由器设置中,IndexRoute的组件工作正常但是当我尝试访问注册页面时,我得到错误"不能GET /singup"。暗示我在哪里犯了错误?

依赖关系:

"dependencies": {
    "express": "^4.16.2",
    "nodemon": "^1.14.12",
    "path": "^0.12.7",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-router": "^3.0.5",
    "webpack": "^3.10.0",
    "webpack-dev-middleware": "^2.0.4"
  }

routes.js

import React from 'react'
import { Route, IndexRoute } from 'react-router';

import App from './components/App';
import Greetings from './components/Greetings';
import SignupPage from './components/signup/SignupPage';

export default(

    <Route path="/" component={App} >
        <IndexRoute component={Greetings} />
        <Route path="signup" component={SignupPage} />
    </Route>

) 

App.js

import React, { Component } from 'react';
import NavigationBar from './NavigationBar'


class App extends Component {
    render() {
        return (
            <div className="container">
                <NavigationBar />
                {this.props.children}
            </div>  
        );
    }
}

export default App;

SignupPage

import React, { Component } from 'react';

class SignupPage extends Component {
    render() {
        return (
            <div>
                <h1>hey iam sign up page</h1>
            </div>
        );
    }
}

export default SignupPage;

2 个答案:

答案 0 :(得分:1)

尝试像这样包装路由器组件

import { Router, Route, browserHistory, IndexRoute } from 'react-router';

<Router history={browserHistory}>
    <Route path="/" component={App} >
        <IndexRoute component={Greetings} />
        <Route path="signup" component={SignupPage} />
    </Route>
</Router>

答案 1 :(得分:0)

感谢回复人员,从node.js提供文件时出错了

这里我只为&#39; /&#39;设置路径,所以当我发送&#39; /注册&#39;的请求时,我收到错误&#39;无法获取/注册& #39;因为没有相同的请求映射

unsigned char fileData[] = {0x01,'S','O','0','0',0x04},

为了解决这个问题,我必须为任何请求网址设置请求映射

app.get('/',(req,res) => {
        // send file as response
        // __dirname -- > points to current director
        res.sendFile(path.join(__dirname,'./index.html'));
    })