You can use async/await to handle asynchronous tasks in Vue.js methods, lifecycle hooks, or Vuex actions.
// MyComponent.vue
export default {
data() {
return {
data: null,
error: null
};
},
mounted() {
this.fetchData();
},
methods: {
async fetchData() {
try {
const response = await this.$axios.get('https://api.example.com/data');
this.data = response.data;
} catch (error) {
this.error = 'Error fetching data';
}
}
}
};