Vue.js Error: 'ReferenceError: axios is not defined' - Solution and Example
This error message indicates an issue with using axios in a Vue.js application. The likely cause is that the axios library hasn't been properly imported into the Vue component where it's being used. To fix this, ensure you've installed the axios library and imported it correctly.
Here's how to import axios into a Vue component:
<template>
<div>
<button @click='getData'>Get Data</button>
</div>
</template>
<script>
import axios from 'axios';
export default {
methods: {
getData() {
axios.get('/api/data')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
}
}
}
</script>
In this example, axios is imported at the top of the component script using the import statement. You can then use it within the getData method to make HTTP requests to an API endpoint.
原文地址: https://www.cveoy.top/t/topic/nKnw 著作权归作者所有。请勿转载和采集!