1

I have this code in my ready method: this.$http.get('url',{},{ headers: { "X-App-Token": "token" } }).then( (data) => this.$set('cdata',data.data)) .catch( (error) => console.log('Got a problem'+error));

and its working well, the probelm is when i move this to a another functtion in methods object it doesn't work.

ready(){
this.getJsonData();
},

methods: {
getJsonData: () => {
this.$http.get('url',{},{
  headers: {
        "X-App-Token": "token"
     }
  }).then(  (data) => this.$set('cdata',data.data))
    .catch( (error) => console.log('Got a problem'+error));
},
},

The error:

src\src\App.vue:23 Uncaught TypeError: Cannot read property '$http' of undefined

//this becomes undefined.

2 Answers 2

1

For anyone trying to solve this you are missing vue.use()

To solve this you need to add

import VueResource from 'vue-resource';
Vue.use(VueResource);

To your main.ts and it should be all fixed

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

Comments

0

I think it may be the use of the arrow to generate the function. May be causing that function to be in an undefined scope? Try this:

methods: {
  getJsonData(){
    this.$http.get('url',{},{
      //...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.