2017-06-15 118 views
0

應用的要求是這樣的:導航到用戶登錄成功的主頁反應+終極版+反應路由器,終極版

1)具有簡單的用戶名和密碼
2)登錄表單一旦用戶提交登錄表單用戶應導航到家庭部件

爲了做到這一點,我已經配置終極版與反應路由器-終極版

問題:
在發送一個正確的用戶名和密碼,服務器認證併發送正確的標記,但現在我成功地顯示URL正確的路由,但分量不加載,

這裏是我的文件

LoginForm的

import React, { Component } from 'react' 
import { Button, Form, FormGroup, FormControl, ControlLabel, Checkbox, Col } from 'react-bootstrap'; 
import { bindActionCreators } from 'redux'; 
import { connect } from 'react-redux'; 
import { userAuthentication } from '../actions/userActions'; 


class LoginForm extends Component { 

    constructor(props) { 
     super(props) 
     this.state = { 
      email: '', 
      password: '' 
     } 
     this.onChange = this.onChange.bind(this); 
     this.onFormSubmit = this.onFormSubmit.bind(this); 
    } 


    onChange(e) { 
     this.setState({ [e.target.name]: e.target.value }); 
    } 

    onFormSubmit(e) { 
     e.preventDefault(); 
     this.props.userAuthentication(this.state) 
    } 

    render() { 
     //const { user, token } = this.props.userState; 
     const formInstance = (
      <div> 
       {/*{token ? <p>{token}</p> : "null"}*/} 
       <Form horizontal onSubmit={this.onFormSubmit}> 
        <FormGroup controlId="formHorizontalEmail"> 
         <Col componentClass={ControlLabel} sm={2}> 
          Email 
        </Col> 
         <Col sm={5}> 
          <FormControl type="email" name="email" onChange={this.onChange} value={this.state.email} placeholder="Email" /> 
         </Col> 
        </FormGroup> 
        <FormGroup controlId="formHorizontalPassword"> 
         <Col componentClass={ControlLabel} sm={2}> 
          Password 
        </Col> 
         <Col sm={5}> 
          <FormControl type="password" name="password" onChange={this.onChange} value={this.state.password} placeholder="Password" /> 
         </Col> 
        </FormGroup> 
        <FormGroup> 
         <Col smOffset={2} sm={10}> 
          <Checkbox>Remember me</Checkbox> 
         </Col> 
        </FormGroup> 
        <FormGroup> 
         <Col smOffset={2} sm={10}> 
          <Button type="submit"> 
           Sign in 
         </Button> 
         </Col> 
        </FormGroup> 
       </Form> 
      </div> 
     ); 
     return (formInstance); 
    } 
} 


function mapDispatchToProps(dispatch) { 
    return bindActionCreators({ userAuthentication: userAuthentication }, dispatch) 
} 

export default connect(null, mapDispatchToProps)(LoginForm); 

行動

import { WS_URL } from '../utill/types'; 
import axios from 'axios'; 
import { push } from 'react-router-redux' 

export function userAuthentication(userData){ 
    return function (dispatch) { 
     dispatch({type: 'USER_LOGIN'}); 
     axios.post(WS_URL+'/authenticate', 
     userData, 
     {headers:{ 
      'Access-Control-Allow-Origin': '*' 
     }} 
     ) 
     .then(function (response){ 
      dispatch({type: 'USER_LOGIN_COMPLETED', payload: response.data}) 
      dispatch(push('/home')) 
      //push('/home') 
     }) 
     .catch(function (error){  
      dispatch({type: 'USER_LOGIN_REJECTECTED', payload: error}) 
     }); 
    } 
} 

index.js

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import { Provider } from "react-redux" 
import createHistory from 'history/createBrowserHistory' 
import { Route } from 'react-router' 
import { ConnectedRouter } from 'react-router-redux' 
//import Routes from './js/utill/routes' 
import registerServiceWorker from './registerServiceWorker'; 
import './css/index.css'; 
import store from "./store" 
import App from './js/components/App'; 
import Home from './js/components/Home'; 
import LoginPage from './js/containers/LoginPage'; 

const history = createHistory() 

ReactDOM.render(<Provider store={store}> 
    <ConnectedRouter history={history}> 
     <div> 
      <Route exact path="/" component={App}/> 
      <Route path="/login" component={LoginPage}/> 
      <Route path="/home" component={Home}/> 
     </div> 
    </ConnectedRouter> 
</Provider> 
, document.getElementById('root')); 
registerServiceWorker(); 

store.js

import { applyMiddleware, createStore } from 'redux' 
import logger from 'redux-logger' 
import { routerMiddleware,routerReducer } from 'react-router-redux'; 
import thunk from "redux-thunk" 
import reducers from './js/reducers' 
import createHistory from 'history/createBrowserHistory' 
import { combineReducers } from 'redux' 

const history = createHistory() 

const combinedReducers = combineReducers({ 
    ...reducers, 
    router: routerReducer 
    }) 

const middleware = applyMiddleware(logger , thunk, routerMiddleware(history)); 

export default createStore(combinedReducers, middleware) 

依賴列表

"axios": "^0.16.2", 
    "history": "^4.6.2", 
    "react": "^15.6.0", 
    "react-bootstrap": "^0.31.0", 
    "react-dom": "^15.6.0", 
    "react-facebook-login": "^3.6.1", 
    "react-google-login": "^2.9.2", 
    "react-redux": "^5.0.5", 
    "react-router-redux": "^5.0.0-alpha.6", 
    "redux": "^3.6.0", 
    "redux-logger": "^3.0.6", 
    "redux-promise": "^0.5.3", 
    "redux-thunk": "^2.2.0" 

有人能幫我爲什麼這個路由不工作,

+0

你有'react-router-redux'的正確版本嗎?最新版本似乎是v4.1.1 [這裏](https://github.com/ReactTraining/react-router/tree/master/packages/react-router-reux) – stafamus

+0

@stafamus是的,這似乎是最新的一個,當我安裝它指向這一點,我有同樣的問題,它似乎是 –

回答

0

我personnaly使用 '反應路由器' 庫react/redux應用程序,它工作正常...

import { browserHistory } from 'react-router'; 

browserHistory.push('/home'); 
+0

我忘了,你需要指定此時啓動你的開發與webpack的開發服務器(如果你使用它):webpack-dev-server - -history-api-fallback「, –

+0

但是目前我們沒有配置webpack的權限,最新的反應配置? –

+0

我不確定我是否有你的問題,但我只是想提一提,我需要調整我的webpack dev服務器配置爲讓它起作用 –