1

I want when I submit the email and the password to login, it will be redirected to the dashboard, my code is below :

     handleClick(event){
    
    var payload={
      "email":this.state.email,
        "password":this.state.password
    }
    axios({
          method: 'post',
          url: '/app/login/',
          data: payload,
          withCredentials: true,
          headers: {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json',
            'Accept': 'application/json',
          }
        })
   
   .then(function (response) {
     console.log(response);
     if(response.data.code == 200){
          response.redirect('http://localhost:5000/#/dashboard');

     }
     else if(response.data.code == 204){
       swal("Erreur !", "Vérifiez vos champs SVP !", "error");
      
     }
     else{
       swal("Erreur !", "Username inexistant !", "error");
      
     }
   })
  }

My route :

import React from 'react';
import Loadable from 'react-loadable'
const Dashboard = Loadable({
  loader: () =>
    import ('./views/Dashboard'),
  loading: Loading,
});
const routes = [
{ path: '/dashboard', name: 'Dashboard', component: Dashboard }]

When I run it, I get : enter image description here

How can I fix that please ?

4

2 回答 2

2

axios 响应对象中没有redirect方法。为了解决这个问题,您可以使用您使用的路由器进行锻炼。

例子:

router.push({ path: '/dashboard' }) 像这样...

编辑:

如果您使用react-router: 尝试history从组件道具中提取,如下所示:

this.props.history.push('/dashboard')
于 2018-09-03T17:38:55.873 回答
2

用于withRouter访问组件中的历史道具:

这是工作示例:https ://codeshare.io/5OZbdW

于 2018-09-03T18:33:32.943 回答