|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <div class="actions"> |
| 4 | + <button |
| 5 | + class="button is-primary" |
| 6 | + @click="startWithSuccess" |
| 7 | + :disabled="isLoading" |
| 8 | + v-text="isInit ? 'Fetch again!' : 'Fetch'" |
| 9 | + /> |
| 10 | + <button |
| 11 | + class="button is-info" |
| 12 | + @click="startWithFailed" |
| 13 | + :disabled="isLoading" |
| 14 | + v-text="`Fetch with failure`" |
| 15 | + /> |
| 16 | + <button |
| 17 | + class="button is-danger" |
| 18 | + @click="stop" |
| 19 | + :disabled="!isLoading" |
| 20 | + v-text="`Abort fetch`" |
| 21 | + /> |
| 22 | + </div> |
| 23 | + |
| 24 | + <!-- isAborted --> |
| 25 | + <use-fetch-demo-table status="Aborted" v-if="isAborted"> |
| 26 | + Resource fetch aborted with status code |
| 27 | + <strong>{{ status }}</strong> and message |
| 28 | + <strong>{{ statusText }}</strong> |
| 29 | + </use-fetch-demo-table> |
| 30 | + |
| 31 | + <!-- isFailed --> |
| 32 | + <use-fetch-demo-table status="Failed" v-else-if="isFailed"> |
| 33 | + Resource fetch failed with status code |
| 34 | + <strong>{{ status }}</strong> and message |
| 35 | + <strong>{{ statusText }}</strong> |
| 36 | + </use-fetch-demo-table> |
| 37 | + |
| 38 | + <!-- isSuccess --> |
| 39 | + <div v-else> |
| 40 | + <!-- !isInit --> |
| 41 | + <use-fetch-demo-table status="Not initialized" v-if="!isInit"> |
| 42 | + Click "Fetch" to initialize the request. |
| 43 | + </use-fetch-demo-table> |
| 44 | + |
| 45 | + <!-- isLoading --> |
| 46 | + <use-fetch-demo-table status="Loading" v-else-if="isLoading"> |
| 47 | + Resource is being fetched... |
| 48 | + </use-fetch-demo-table> |
| 49 | + |
| 50 | + <!-- Fetched --> |
| 51 | + <use-fetch-demo-table status="Success" v-else> |
| 52 | + <p> |
| 53 | + Resource fetched successfully with status code |
| 54 | + <strong>{{ status }}</strong> and message |
| 55 | + <strong>{{ statusText }}</strong> |
| 56 | + </p> |
| 57 | + <img class="img" :src="data.message" alt="" /> |
| 58 | + </use-fetch-demo-table> |
| 59 | + </div> |
| 60 | + </div> |
| 61 | +</template> |
| 62 | + |
| 63 | +<script lang="ts"> |
| 64 | +import Vue from 'vue' |
| 65 | +import { ref } from '@src/api' |
| 66 | +import { useFetch } from '@src/vue-use-kit' |
| 67 | +import UseFetchDemoTable from './UseFetchDemoTable.vue' |
| 68 | +
|
| 69 | +export default Vue.extend({ |
| 70 | + name: 'UseFetchDemo', |
| 71 | + components: { UseFetchDemoTable }, |
| 72 | + setup() { |
| 73 | + const isInit = ref(false) |
| 74 | + const slowApi = 'http://slowwly.robertomurray.co.uk/delay/1000/url' |
| 75 | + const randomDogUrl = `${slowApi}/https://dog.ceo/api/breeds/image/random` |
| 76 | + const url = ref(randomDogUrl) |
| 77 | + const { |
| 78 | + data, |
| 79 | + status, |
| 80 | + statusText, |
| 81 | + isLoading, |
| 82 | + isFailed, |
| 83 | + isAborted, |
| 84 | + start, |
| 85 | + stop |
| 86 | + } = useFetch(url, {}, false) |
| 87 | +
|
| 88 | + const startWithSuccess = () => { |
| 89 | + isInit.value = true |
| 90 | + url.value = randomDogUrl |
| 91 | + start() |
| 92 | + } |
| 93 | +
|
| 94 | + const startWithFailed = () => { |
| 95 | + isInit.value = true |
| 96 | + url.value = `${slowApi}/https://dog.ceo` |
| 97 | + start() |
| 98 | + } |
| 99 | +
|
| 100 | + return { |
| 101 | + data, |
| 102 | + status, |
| 103 | + statusText, |
| 104 | + isInit, |
| 105 | + isLoading, |
| 106 | + isFailed, |
| 107 | + isAborted, |
| 108 | + startWithSuccess, |
| 109 | + startWithFailed, |
| 110 | + stop |
| 111 | + } |
| 112 | + } |
| 113 | +}) |
| 114 | +</script> |
| 115 | + |
| 116 | +<style scoped> |
| 117 | +.actions { |
| 118 | + padding-bottom: 20px; |
| 119 | +} |
| 120 | +
|
| 121 | +.img { |
| 122 | + display: block; |
| 123 | + margin: 20px 0 0; |
| 124 | + max-width: 300px; |
| 125 | + border-radius: 3px; |
| 126 | +} |
| 127 | +</style> |
0 commit comments