【问题标题】:How to history with Axios in React如何在 React 中使用 Axios 进行历史记录
【发布时间】:2020-07-24 07:03:19
【问题描述】:

我正在尝试创建一个登录页面,该页面从 url 接收登录数据,然后在用户输入正确的登录详细信息时重定向。但是,当登录成功时(flag = True),我无法重定向。

  import React,{Component} from 'react'
import {Form,Button} from 'react-bootstrap'
import { Link } from 'react-router-dom';
import {Navbar,Nav} from 'react-bootstrap'
import axios from 'axios'


export default class LoginPage extends Component
{
    constructor(props)
    {
        super(props)
        this.state = {emailValue: '', password: '', users: [], nextPageURL: ' '}
        this.handleChanges = this.handleChanges.bind(this)
        this.handleSubmit = this.handleSubmit.bind(this)
        
        this.URL = XXXXXXX
    }


    componentDidMount()
    {

    }

    handleChanges(event)
    {
        if(event.target.name === 'password')
            this.setState({emailValue: this.state.emailValue, password: event.target.value})
        else if(event.target.name === 'username')
            this.setState({emailValue: event.target.value, password: this.state.password})
    }

    handleSubmit(event)
    {

       if(this.state.emailValue === '')
            alert('You forgot to type your email')
        else if(this.state.password === '')
            alert('You forgot to type your password')
        else
        {
            var email = this.state.emailValue
            var pw = this.state.password
            var flag = false
            var self = this

            axios.get(this.URL)
            .then(function(response)
            {
                for(var i = 0; i < response.data.length; i++)
                    if(email === response.data[i]['email'] && pw === response.data[i]['password'])
                        flag = true
                
            })
            .then(function(response)
            {
                if(!flag) 
                    alert('Login Unsucessful')
                else 
                    LoginPage.props.history.push('/')
            })

        } 

    } 

    render()
    {
        const formStyles = {
                padding: '150px',

        }



        return (
            <div>
                <Navbar bg="light" expand="lg">
                            <Navbar.Toggle aria-controls="basic-navbar-nav" />
                            <Navbar.Collapse id="basic-navbar-nav">
                                <Navbar.Brand href="/" >WatchDog</Navbar.Brand>

                                <Nav className = "justify-content-between" style={{ width: "100%" }}>
                                    <Navbar.Text> Please login to your Watchdog Account </Navbar.Text>
                                </Nav>

                                <Nav className = "justify-content-end"  style={{ width: "100%" }}> 
                        
                                        

                                </Nav>
                            </Navbar.Collapse>
                    </Navbar>

                <Form float ="justify-content-center" style = {formStyles} onSubmit = {this.handleSubmit}>
                    <Form.Group controlId="formBasicEmail">
                        <Form.Label>Email address</Form.Label>
                        <Form.Control type="email" placeholder="Enter email" name="username" onChange = {this.handleChanges}/>
                        <Form.Text className="text-muted">
                        We'll never share your email with anyone else.
                        </Form.Text>
                    </Form.Group>

                    <Form.Group controlId="formBasicPassword">
                        <Form.Label>Password</Form.Label>
                        <Form.Control type="password" placeholder="Password"  name = "password"  onChange = {this.handleChanges}/>
                    </Form.Group>

                    <Button variant="primary" type="submit"> Submit </Button>
                </Form> 


            </div>
        ) 
    }
}

我正在使用 React 浏览器路由器。

谁能解释我做错了什么以及如何解决它?感谢您的时间和考虑。

【问题讨论】:

  • 尝试this.props 而不是LoginPage.props
  • 能否请您包含组件的整个代码?
  • 对不起@Rostyslav,只是这样做了。 anshu多功能,试过了,没用。

标签: javascript reactjs axios


【解决方案1】:

修改代码如下:

axios.get(this.URL)
.then(function(response)
{
    for(var i = 0; i < response.data.length; i++)
        if(email === response.data[i]['email'] && pw === response.data[i]['password'])
            flag = true
}) 
.then(res => {
    if(!flag) 
        alert('Login Unsucessful')
    else 
        LoginPage.props.history.push('/')
})

【讨论】:

  • 你好@Dishant,我刚试过。它没有重定向。它只是停留在同一页面上。
  • 重定向到登录页面时是否遇到任何错误?
猜你喜欢
  • 2021-10-29
  • 1970-01-01
  • 2017-12-08
  • 2021-05-12
  • 2018-05-10
  • 2010-09-30
  • 2020-10-16
  • 2017-07-30
相关资源
最近更新 更多