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');
0

2 Answers 2

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)
    
Sign up to request clarification or add additional context in comments.

Comments

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;
 }
}

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.