I created a simple Vue app using vue-cli 3 and configured with TypeScript. I also installed axios and I'm trying to use it in mounted()
in order to load data and display it in a component.
Here's the code:
<script lang='ts'>
import { Component, Prop, Vue } from 'vue-property-decorator';
import SearchBar from '@/components/prods_comp/SearchBar.vue';
import ProdsTable from '@/components/prods_comp/ProdsTable.vue';
@Component({
components: {
SearchBar,
ProdsTable,
},
})
export default class MainProds extends Vue {
@Prop({ default: 'Produits' }) private message!: string;
mounted() {
const baseURI = 'http://localhost:8069';
this.$http({
method: 'POST',
url: baseURI + '/vue_web_services/msg/',
headers: { 'content-type': 'application/json',},
data: {
}
}).then(function (response) {
console.log(response.data);
});
}
}
</script>
This is a fairly basic API call. It works. The problem is this message that keeps on popping in the console:
ERROR in D:/ODOO/Vue/bons_commande/src/components/prods_comp/MainProds.vue
26:10 Property '$http' does not exist on type 'MainProds'.
24 | mounted() {
25 | const baseURI = 'http://localhost:8069';
> 26 | this.$http({
| ^
27 | method: 'POST',
28 | url: baseURI + '/vue_web_services/msg/',
29 | crossdomain: true,
EDIT: Here's main.ts:
import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
import VueSweetalert2 from 'vue-sweetalert2';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import '@/assets/scss/tailwind.scss';
import axios from 'axios';
Vue.config.productionTip = false;
Vue.prototype.$http = axios;
Vue.use(ElementUI);
Vue.use(VueSweetalert2);
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app');
It looks like you might be missing vue-axios
, which adds $http
to the Vue prototype, allowing this.$http()
calls from single file components.
To address the issue:
Install vue-axios
from the command line:
npm i -S vue-axios
Add the following code in main.ts
:
import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)