vue-resource和vue-axios的簡單使用方法


兩者其實差別不大,都是基於es6的Promise對象實現的方法

vue-resource:

main.js =>

import Vue from 'vue';

import VueResource from 'vue-resource';

Vue.use(VueResource);

組件里面使用=>

<template>
    <div>{{myData}}</div>
</template>
<script>
    export default {
        data() {
            return {
                myData:{}
            }
        },
        created() {      
            this.$http.get(url).then((response) => {
                // success
              this.myData = response.body.data;
            },(error) => {
                // error
                console.log(error)
            });
        }
    }
</script>

官方傳送門

vue-axios:

axios不能使用use方法

main.js=>

import Vue from 'vue';

import axios from 'axios';

Vue.prototype.$axios = axios;

組件里面使用=>

<template>
  <div>{{myData}}</div>
</template>
<script>
export default {
  data() {
      return {
        myData: {}
      }
    },
    created() {
      this.$axios.get(url).then((response) => {
        // success
        this.myData = response.data.data;
      }, (error) => {
        // error
        console.log(error)
      });
    }
</script>

官方傳送門


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM