axios 如何取消网络请求

55 阅读1分钟
<script>
import { userList } from 'api'
export default {
data() {
  sourceL null,
  dataList: []
},
methods: {
    cancelQuest() {
      if (typeof this.source === 'function') {
        this.source('终止请求'); //取消请求
      }
    },
    getData() {
        this.cancelQuest() //取消上一次请求
        userList({name: '', pageNum: 1, pageSize: 10}, this).then((res) => {
            this.dataList = res.data
         })
    }
}
}
</script>
export function userList(Obj,that) {
  return axios({
    method:'POST',
    url: "xxxxxx",
    data:Obj,
    cancelToken: new AXIOS.CancelToken(function executor(c) {
      that ? that.source = c : ''
    })
  })
}