我在前端使用Reactjs在后端使用laravel开发了一个Singnup页面,我希望当我单击按钮注册时,它将重定向到我的登录页面。
我的注册组件是:
handleSubmit = event => {
event.preventDefault();
const users = {
name:this.state.name,
email:this.state.email,
cin:this.state.cin,
phoneNumber:this.state.phoneNumber,
birthDate:this.birthDate,
password:this.state.password
}
console.log(users)
axios({
method: 'post',
url: 'http://172.16.234.24:8000/api/register',
data: users,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Accept': 'application/json',
}
})
.then(function (response) {
console.log(response);
this.props.history.push('/login');
})
.catch(function (response) {
console.log(response);
});
}
我的路线是:
import React from "react";
import { Route, Switch, } from "react-router-dom";
import Home from "./Home";
import NotFound from "./NotFound";
import Login from "./Login";
import Signup from "./Signup";
export default () =>
<Switch>
<Route path="/" exact component={Home} />
<Route path="/login" exact component={Login} />
<Route path="/signup" exact component={Signup} />
<Route component={NotFound} />
</Switch>;
我的索引是:
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router } from "react-router-dom";
import App from './App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(
<Router>
<App />
</Router>
, document.getElementById('root'));
serviceWorker.unregister();
我的App.js是:
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import { Nav, Navbar, NavItem } from "react-bootstrap";
import { LinkContainer } from "react-router-bootstrap";
import Routes from "./Routes";
import './App.css';
class App extends Component {
render() {
return (
<div className="App container">
<Navbar fluid collapseOnSelect>
<Navbar.Header>
<Navbar.Brand>
<Link to="/">Home</Link>
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse>
<Nav pullRight>
<LinkContainer to="/signup">
<NavItem>Signup</NavItem>
</LinkContainer>
<LinkContainer to="/login">
<NavItem>Login</NavItem>
</LinkContainer>
</Nav>
</Navbar.Collapse>
</Navbar>
<Routes />
</div>
);
}}
export default App;
我的react-router-dom版本是4.3.1,运行时我得到了:
typeerror: cannot read property 'props' of undefined
我该如何解决?
答案 0 :(得分:1)
您应将注册组件与withRouter
的高阶组件一起包装,以访问this.props.history
。
答案 1 :(得分:1)
只需测试一下-重定向就可以了。
(在“注册”组件中)。
import React, { Component } from "react";
const axios = require('axios');
export default class Signup extends Component {
constructor(props) {
super(props);
this.state = {}
}
handleSubmit = event => {
event.preventDefault();
axios({
method: 'get',
headers: {
"Content-type": "application/json"
},
url: 'https://jsonplaceholder.typicode.com/todos/1'
}).then(response => {
console.log(response);
this.props.history.push('/login');
})
.catch(response => {
console.log(response);
});
}
render() {
return(
<form onSubmit={this.handleSubmit}>
<button
type="submit">
yo
</button>
</form>
)
}
}
尝试使用上面的占位符代码来证明重定向能够正常运行,然后用您的自定义API + POST
替换。注意:您可能需要stringify
POST
负载。
我们不知道您的App.js是什么样子,但是您的App.js还需要包含<Router />
组件。
答案 2 :(得分:1)
尝试在axios调用之前隔离属性:
let { history } = this.props
axios({
method: 'post',
url: 'http://172.16.234.24:8000/api/register',
data: users,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Accept': 'application/json',
}
})
.then(function (response) {
console.log(response);
history.push('/login');
})
.catch(function (response) {
console.log(response);
});