I’m using vuejs and I’m trying to make a GET request to my API. I’m using axios, but I cannot import it, if I use require to import axios i get this error: Uncaught ReferenceError: require is not defined.
This is my vuetest.js file:
const axios = require("axios");
new Vue({
el: "#rentalsVue",
data() {
return {
info: null,
};
},
mounted() {
axios.get("/getRentals").then((response) => (this.info = response));
},
});
This is the html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div id="rentalsVue">
{{ info }}
</div>
<script src="vuetest.js"></script>
</body>
</html>
>Solution :
This happens because require() doesn’t exist on browser/client-side JavaScript.
You need to import Axios using the <script> tag and then you will be able to use it.