sunmingtao / sample-code

3 stars 4 forks source link

Vue js vue-resource steps (send http request) #109

Closed sunmingtao closed 4 years ago

sunmingtao commented 4 years ago

npm install --save vue-resource

main.js

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

Vue.http.options.root="http://localhost:8080"

App.vue

With root configured:

this.$http.get("leagues").then(response => {
      this.leagues = response.data
    });

Without root configured:

this.$http.get('https://age-pension.firebaseio.com/refData.json')
        .then(response => {
          return response.json();
        }).then(data => {
          this.pensionReferenceData = data.pension;
          this.currencyReferenceData = data.currency;
          this.isDataFetched = true;
        });

this.$http.post('https://age-pension.firebaseio.com/refData.json', this.user)
        .then(response => {
          console.log(response);
        }, error => {
            console.log(error);
        });
sunmingtao commented 4 years ago

Set up base URL

main.js

Vue.http.options.root = "https://age-pension.firebaseio.com/refData.json"

App.vue

this.$http.get('').then(...);