Skip to content

Instantly share code, notes, and snippets.

@mholtzhausen
Created March 26, 2020 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mholtzhausen/6083e379a9f4c0e338d685f58f7044e0 to your computer and use it in GitHub Desktop.
Save mholtzhausen/6083e379a9f4c0e338d685f58f7044e0 to your computer and use it in GitHub Desktop.
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