Window.locatin防止数据删除

时间:2019-04-22 11:44:01

标签: javascript reactjs axios

我有一个按钮,当按下该按钮时,我想删除该对象。这是我的按钮:

<Button onClick={this.handleDelete.bind(this)}> 
    Delete 
</Button>

这是我的handleDelete

handleDelete() {
    axios
      .delete(
        `http://proiectdepozite.laravel/api/clienti/${this.state.client.id}`
      )
      .then((window.location = "/clienti"))
      .catch(error => console.log(error));
  }

现在,如果我按下按钮,它将把我重定向到我想去的地方,但不会删除任何内容。我尝试注释掉.then(....),它可以正常工作。错误在哪里?

2 个答案:

答案 0 :(得分:3)

。然后方法需要回调函数,但您只需传递一些表达式即可。

handleDelete() {
    axios
      .delete(
        `http://proiectdepozite.laravel/api/clienti/${this.state.client.id}`
      )
      //try this
      .then(()=>(window.location = "/clienti"))
      .catch(error => console.log(error));
  }
//use async await
async handleDelete() {
   try{
    let result = await axios.delete(
       `http://proiectdepozite.laravel/api/clienti/${this.state.client.id}`
      )

     if(result){
       //success logic 
       window.location = "/clienti"
     }

  }
  catch(e){
    throw e;
  }
}

答案 1 :(得分:2)

axios.delete('url', { data: payload }).then(
  // Observe the data keyword  Very important
  // payload is the request body

   ()=>(window.location = "/clienti")//you can pass as pipe function


)