vue發送ajax請求


一、vue-resource

1、簡介

  一款vue插件,用於處理ajax請求,vue1.x時廣泛應用,現不被維護。

2、使用流程

step1:安裝

【命令行輸入】
npm install vue-resource --save

step2:引入

【main.js】
// 引入vue-resource
import VueResource from 'vue-resource'

// 使用vue-resource
Vue.use(VueResource)

step3:編碼

【格式:】
    this.$http.get().then()    返回的是一個Promise對象   

step4:完整代碼

【使用vue-cli創建項目】
https://www.cnblogs.com/l-y-h/p/11241503.html

【main.js】
import Vue from 'vue'
import App from './App.vue'
// 引入vue-resource
import VueResource from 'vue-resource'

// 使用vue-resource
Vue.use(VueResource)
Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')


【App.vue】
<template>
    <div>
        <div v-if="!repositoryUrl">loading...</div>
        <div v-else>most star repository is <a :href="repositoryUrl">{{repositoryName}}</a></div>
    </div>
    <!--App -->
</template>

<script>
    export default {
        data() {
            return {
                repositoryUrl : '',
                repositoryName : ''
            }
        },
        
        mounted() {
            // 發ajax請求,用以獲取數據,此處地址意思是查詢 github中 vue 星數最高的項目
            const url = 'https://api.github.com/search/repositories?q=vue&sort=stars';
            this.$http.get(url).then(
                response => {
                    const result = response.data.items[0];
                    console.log(result)
                    this.repositoryUrl = result.html_url;
                    this.repositoryName = result.name;
                },

                response => {
                    alert('請求失敗');
                },
            );
        }
    }
</script>

<style>

</style>

step5:截圖:

 

 請求正常

 

 點擊鏈接跳轉

 

 使用錯誤的地址

 

 彈出錯誤提示框

 

 

二、axios

1、簡介

  一款vue庫,用於處理ajax請求,vue2.x時廣泛應用。

2、流程

step1:安裝

【命令行輸入】
npm install axios --save

step2:引入

【在哪里使用,就在哪里引入】
import axios from 'axios';

step3:完整代碼

【main.js】
import Vue from 'vue'
import App from './App.vue'

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')



【App.vue】
<template>
    <div>
        <div v-if="!repositoryUrl">loading...</div>
        <div v-else>most star repository is <a :href="repositoryUrl">{{repositoryName}}</a></div>
    </div>
    <!--App -->
</template>

<script>
    import axios from 'axios';
    
    export default {
        data() {
            return {
                repositoryUrl : '',
                repositoryName : ''
            }
        },
        
        mounted() {
            // 發ajax請求,用以獲取數據,此處地址意思是查詢 github中 vue 星數最高的項目
            const url = 'https://api.github.com/search/repositories?q=vue&sort=stars';
            
            axios.get(url).then(
                response => {
                    const result = response.data.items[0];
                    console.log(result)
                    this.repositoryUrl = result.html_url;
                    this.repositoryName = result.name;
                }
            ).catch(
                response => {
                    alert('請求失敗');
                },
            );
        }
    }
</script>

<style>

</style>

step5:截圖與上面的 vue-resource 一樣,此處不重復截圖。

 

3、axios 解決跨域問題

參考: https://www.cnblogs.com/l-y-h/p/11815452.html

 

4、axios 項目中封裝使用

參考:

  https://www.cnblogs.com/l-y-h/p/12955001.html#_label1

  https://www.cnblogs.com/l-y-h/p/12973364.html#_label1


免責聲明!

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



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