Vue.js – Cannot read property ‘get’ of undefined

Trying to get a http request in Vue.js:

this.$http.get('ApiURL')

I got error: Cannot read property ‘get’ of undefined

After a long, long searching I found the solution.

In your main.js file bold lines are the solution:

//your imports...
import vueResource from 'vue-resource'

Vue.use(vueResource)

new Vue({
  //your vue configuration
})

And then you can use Vue.http in your methods:

methods: {
  yourMethod: function () {
    Vue.http.get('yourApiURL').then((response) => {
        console.log('data: ', response.body)
        this.yourDataObject = response.body
    })
  }
}

You are welcome!

Advertisements