VueJS + Typescript: Property does not exist on type
Asked Answered
H

2

12

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');
Hulbard answered 4/2, 2019 at 16:3 Comment(0)
C
9

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:

  1. Install vue-axios from the command line:

    npm i -S vue-axios
    
  2. Add the following code in main.ts:

    import Vue from 'vue'
    import axios from 'axios'
    import VueAxios from 'vue-axios'
    
    Vue.use(VueAxios, axios)
    
Citified answered 6/2, 2019 at 1:20 Comment(0)
A
5

Make a plugin file like below and use this plugin in main.ts

For detailed documentation -- https://v2.vuejs.org/v2/guide/typescript.html#Augmenting-Types-for-Use-with-Plugins

import _Vue from 'vue';
import Axios from 'axios';
export function AxiosPlugin<AxiosPlugOptions>(Vue: typeof _Vue, options?: AxiosPluginOptions): void {
   // do stuff with options
   Vue.prototype.$http = Axios;
 }
export class AxiosPluginOptions { // add stuff } 
import { AxiosStatic } from 'axios';
declare module 'vue/types/vue' {
   interface Vue {
     $http: AxiosStatic;
 }
}
Apish answered 5/2, 2019 at 10:9 Comment(1)
Thank you for the reply, but I'm not too familiar with custom plugins. How would I go about adding it to my main.ts file?Hulbard

© 2022 - 2024 — McMap. All rights reserved.