Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我正在尝试使用this.history.push重定向到另一个页面,但它不起作用_Javascript_Reactjs_React Router_Axios - Fatal编程技术网

Javascript 我正在尝试使用this.history.push重定向到另一个页面,但它不起作用

Javascript 我正在尝试使用this.history.push重定向到另一个页面,但它不起作用,javascript,reactjs,react-router,axios,Javascript,Reactjs,React Router,Axios,我使用this.context.history.push重定向到另一个页面,但它不起作用。有什么解决办法吗? 我使用laravel作为后端,react js作为前端,现在我想将我的登录页面重定向到供应商配置文件,但它不起作用。 这是我的vendorlogin.jsx import React from 'react' import { vendorlogin } from '../API/UserAPI' export default class VendorLogin extends Reac

我使用this.context.history.push重定向到另一个页面,但它不起作用。有什么解决办法吗? 我使用laravel作为后端,react js作为前端,现在我想将我的登录页面重定向到供应商配置文件,但它不起作用。 这是我的vendorlogin.jsx

import React from 'react'
import { vendorlogin } from '../API/UserAPI'
export default class VendorLogin extends React.Component {
    constructor() {
        super()
        this.state = {
            email: '',
            password: '',
            redirect:false

        }

        this.onChange = this.onChange.bind(this)
        this.onSubmit = this.onSubmit.bind(this)
    }

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

        const vendor = {
            email: this.state.email,
            password: this.state.password
        }

        vendorlogin(vendor).then(res => {
            if (res){
                this.context.history.push('/vp');
            }

            else{
                console.log("login error");
            }
        });
    }

    render() {

        return (
            <div className="container">
                <div className="row">
                    <div className="col-md-6 mt-5 mx-auto">
                        <form noValidate >
                            <h1 className="h3 mb-3 font-weight-normal">
                                Please sign in
                            </h1>
                            <div className="form-group">
                                <label htmlFor="email">Email address</label>
                                <input
                                    type="email"
                                    className="form-control"
                                    name="email"
                                    placeholder="Enter email"
                                    value={this.state.email}
                                    onChange={this.onChange}
                                />
                            </div>
                            <div className="form-group">
                                <label htmlFor="password">Password</label>
                                <input
                                    type="password"
                                    className="form-control"
                                    name="password"
                                    placeholder="Password"
                                    value={this.state.password}
                                    onChange={this.onChange}
                                />
                            </div>
                        </form>
                        <button
                            type="submit"
                            value="LOGIN"
                            className="button"
                            onSubmit={this.onSubmit}
                        >LOGIN</button>
                    </div>
                </div>
            </div>
        )
    }
}


欢迎这与
PHP
有什么关系?对不起,我是新来的,我的网站后端在laravel上,所以我添加了PHP标记。this.history.push用于更改url,而不将用户重定向到其他页面。那么我应该怎么做?欢迎。这与
PHP
有什么关系?很抱歉,我是新来的,我的网站后端在laravel上,所以我添加了PHP标记。this.history.push用于更改url,而不将用户重定向到另一个页面。那么我应该怎么做呢?尝试使用:window.location.replace(“/vp”)也许您的项目有问题,我一直在我的项目中使用window.location,没有任何问题!在控制台中检查您的项目结果或查看您的代码可能有错误。在控制台中也没有显示任何内容。您可以查看我的代码吗?这很紧急。请从代码中删除表单标记,并将onSubmit={this.onSubmit}更改为onClick={this.onSubmit}和try againcode正常工作,但只有重定向部分不工作,当我尝试重定向时,它不会显示任何错误,也不会重定向try with:window.location.replace(“/vp”)可能您的项目有问题,我正在使用window.location始终在我的项目中,没有任何问题!在控制台中检查您的项目结果或查看您的代码可能有错误。在控制台中也没有显示任何内容。您可以查看我的代码吗?这很紧急。请从代码中删除表单标记,并将onSubmit={this.onSubmit}更改为onClick={this.onSubmit}try againcode工作正常,但只有重定向部分不工作,当我尝试重定向时,它不会显示任何错误,也不会重定向
import React from 'react'
import {BrowserRouter as Router,Route} from 'react-router-dom'
import Home from './components/Main/home';
import { Stsignin } from './components/Student/stsignin';
//import { Vsignin } from './components/Vendor/vsignin';
import VendorProfile from './components/Vendor/profile';
import Panel from './components/Vendor/panel';
import Offer from './components/Vendor/offer';
import Vreport from './components/Vendor/vreport';
import Vp from './components/Vendor/vp';
import VendorLogin from './components/Vendor/vendorlogin';



class App extends React.Component {
  render() {
    return (
      <Router>
      <Route path='/home' component={Home}></Route>
      <Route path='/stsignin' component={Stsignin}></Route>

        <Route path='/panel' component={Panel}></Route>
        <Route path='/profile' component={VendorProfile}></Route>
        <Route path='/offer' component={Offer}></Route>
        <Route path='/vreport' component={Vreport}></Route>
        <Route path='/vp' component={Vp}></Route>
        <Route path='/vendorlogin' component={VendorLogin}></Route>
      </Router>
    );
  }
}

export default App;
import axios from 'axios'
export const vendorlogin = vendor => {
    return axios
        .post('api/vendorlogin', {
            email : vendor.email,
            password : vendor.password
        }, {
            headers: { 'Content-Type': 'application/json' }
        })
        .then(response => {
            localStorage.setItem('usertoken',response.data.token)
            console.log(response)
        })
        .catch(err => {
            console.log(err)
        })
    }
vendorlogin(vendor).then(res => {
            if (res){
                //this.context.history.push('/vp');
                window.location = '/vp';
            }

            else{
                console.log("login error");
            }
        });