Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS + Typescript: Property does not exist on type

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');
like image 848
Kyle Sentient Avatar asked Feb 04 '19 16:02

Kyle Sentient


2 Answers

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)
    
like image 100
tony19 Avatar answered Oct 03 '22 09:10

tony19


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

For detailed documentation -- https://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;
 }
}
like image 23
Mohit Tilwani Avatar answered Oct 03 '22 09:10

Mohit Tilwani