Created
March 26, 2020 13:49
-
-
Save mholtzhausen/6083e379a9f4c0e338d685f58f7044e0 to your computer and use it in GitHub Desktop.
This file contains 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
Vue.component("async", { | |
props: { | |
url: { type: String, default: "", required: true }, | |
params: { type: Object, default: () => ({}) } | |
}, | |
data() { | |
return { | |
pending: true, | |
error: false, | |
data: null | |
}; | |
}, | |
watch: { | |
url() { | |
this.requestData(); | |
}, | |
params: { | |
handler() { | |
this.requestData(); | |
}, | |
deep: true | |
} | |
}, | |
mounted() { | |
this.requestData(); | |
}, | |
methods: { | |
async requestData() { | |
this.pending = true; | |
try { | |
const { data } = await axios.get(this.url, { params: this.params }); | |
this.data = data; | |
this.error = false; | |
} catch (e) { | |
this.data = null; | |
this.error = e; | |
} | |
this.pending = false; | |
} | |
}, | |
render() { | |
return this.$scopedSlots.default({ | |
pending: this.pending, | |
error: this.error, | |
data: this.data | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment