2

I have following code

<div id="vue-instance">

</div>

JS

var vm = new Vue({
  el: '#vue-instance',
  data: {
  },
  ready:function(){
    this.loadCountries();
  },
  methods:{
        loadCountries(){
            this.$http.get('https://restcountries.eu/rest/v1/all',function(data){
                console.log(data);
          })
      }
  }
});

When I run the above code it gives me following error

Uncaught TypeError: Cannot read property 'get' of undefined

I have a fiddle

JsFiddle

Help would be much appreciated

4
  • Do you have vue-resource installed and configured? Commented Feb 22, 2017 at 9:25
  • stackoverflow.com/questions/33247812/this-http-vuejs-not-set Commented Feb 22, 2017 at 9:39
  • @AmreshVenugopal how to install that on jsFiddle ? Commented Feb 22, 2017 at 11:07
  • Vue Resource is deprecated. Use Axios Commented Jan 31, 2020 at 7:10

5 Answers 5

6

$http.get is from Vue Resource. Make sure you are pulling that in properly by adding vue-resource to your package.json, install it via npm install and in code:

var Vue = require('vue');

Vue.use(require('vue-resource'));

To use this in fiddle, you have to add vue-resource cdn link in external resources and use following in code:

Vue.use(VueResource)

See working demo: https://jsfiddle.net/49gptnad/187/

Sign up to request clarification or add additional context in comments.

5 Comments

how to do that in the fiddle ? All I want to do is an Ajax Call is there any other way ?
I still can't see anything in the console although I see that the error is gone , and I test manually the url works fine
@Vikram There were few more errors in your code, see working solution here. Let me know in case of any query.
it would be good if you can update that link in the Answer as well . So that its useful for other people
@Saurabh Maybe you can help me. Look at this : stackoverflow.com/questions/52529345/…
4

In order to use $http you must install the vue-resource middleware first. You can use the following command to install it.

npm install vue-resource --save

After its installed go to your main.js file and import the vue resource as follows

import vueResource from 'vue-resource'

Now its imported so we just have to call the use method of Vue to work with this middleware. You can call the use method as follows.

Vue.use(vueResource)

Comments

0

As you have no plugin for vue-resource your this.$http is undefined. To add Vue-resource on js-fiddle add https://cdn.jsdelivr.net/g/[email protected],[email protected] to the external scripts section.

Comments

0

I fixed this issue of the following mode:

npm install bootstrap-vue --save
npm install vue-resource --save

in main.js

import Vue from './bootstrap'
import BootstrapVue from 'bootstrap-vue'

Vue.use(BootstrapVue)

create bootstrap.js

import Vue from 'vue'
import VueResource from 'vue-resource'

Vue.use(VueResource)

export { Vue }

In App.vue

this.$http.get('/api/module/all').then(...

Comments

0

I used another way as of Oct 2019. I first installed using NPM like this

npm install axios --save

I used this below later and doing this below got rid of the error.

import  axios  from 'axios';

Comments

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.