Last active
February 5, 2025 08:47
-
-
Save wobsoriano/56304b19f59f76fb8ca9ed97ecef2529 to your computer and use it in GitHub Desktop.
Axios cancel token as Vue hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<button @click="ping">Ping</button> | |
</template> | |
<script setup> | |
import useCancelToken from './useCancelToken'; | |
import axios from 'axios' | |
const cancelToken = useCancelToken(); | |
const ping = () => { | |
axios.get('https://ping.example.com', { cancelToken }) | |
} | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { onUnmounted } from 'vue'; | |
import axios from 'axios'; | |
export default function useCancelToken() { | |
const source = axios.CancelToken.source(); | |
onUnmounted(() => { | |
source.cancel('Fetch cancelled'); | |
}); | |
return source.token; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment