Vue發(fā)送ajax請(qǐng)求方法介紹
一、vue-resource
1、簡介
一款vue插件,用于處理ajax請(qǐng)求,vue1.x時(shí)廣泛應(yīng)用,現(xiàn)不被維護(hù)。
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() 返回的是一個(gè)Promise對(duì)象
step4:完整代碼
【使用vue-cli創(chuàng)建項(xiàng)目】 http://www.dbjr.com.cn/article/235498.htm 【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" rel="external nofollow" rel="external nofollow" >{{repositoryName}}</a></div> </div> <!--App --> </template> <script> export default { data() { return { repositoryUrl : '', repositoryName : '' } }, mounted() { // 發(fā)ajax請(qǐng)求,用以獲取數(shù)據(jù),此處地址意思是查詢 github中 vue 星數(shù)最高的項(xiàng)目 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('請(qǐng)求失敗'); }, ); } } </script> <style> </style>
step5:截圖:
請(qǐng)求正常
點(diǎn)擊鏈接跳轉(zhuǎn)
使用錯(cuò)誤的地址
彈出錯(cuò)誤提示框
二、axios
1、簡介
一款vue庫,用于處理ajax請(qǐng)求,vue2.x時(shí)廣泛應(yīng)用。
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" rel="external nofollow" rel="external nofollow" >{{repositoryName}}</a></div> </div> <!--App --> </template> <script> import axios from 'axios'; export default { data() { return { repositoryUrl : '', repositoryName : '' } }, mounted() { // 發(fā)ajax請(qǐng)求,用以獲取數(shù)據(jù),此處地址意思是查詢 github中 vue 星數(shù)最高的項(xiàng)目 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('請(qǐng)求失敗'); }, ); } } </script> <style> </style>
step5:截圖與上面的 vue-resource 一樣,此處不重復(fù)截圖。
3、axios 解決跨域問題
參考:http://www.dbjr.com.cn/article/235490.htm
到此這篇關(guān)于Vue發(fā)送ajax請(qǐng)求的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vuejs中監(jiān)聽窗口關(guān)閉和窗口刷新事件的方法
今天小編就為大家分享一篇vuejs中監(jiān)聽窗口關(guān)閉和窗口刷新事件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09Vue實(shí)現(xiàn)簡單可擴(kuò)展甘特圖的方法詳解
Ganttastic是一個(gè)小型的Vue.js組件,用于在Web應(yīng)用程序上呈現(xiàn)一個(gè)可配置的、可拖動(dòng)的甘特圖。本文就將用它來實(shí)現(xiàn)簡單可擴(kuò)展的甘特圖,感興趣的可以嘗試一下2022-11-11vue 解決provide和inject響應(yīng)的問題
這篇文章主要介紹了vue 解決provide和inject響應(yīng)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11Vue 實(shí)現(xiàn)一個(gè)簡單的鼠標(biāo)拖拽滾動(dòng)效果插件
這篇文章主要介紹了Vue 實(shí)現(xiàn)一個(gè)簡單的鼠標(biāo)拖拽滾動(dòng)效果插件,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12vue計(jì)算屬性computed、事件、監(jiān)聽器watch的使用講解
今天小編就為大家分享一篇關(guān)于vue計(jì)算屬性computed、事件、監(jiān)聽器watch的使用講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01Vue清除定時(shí)器setInterval優(yōu)化方案分享
這篇文章主要介紹了Vue清除定時(shí)器setInterval優(yōu)化方案分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07Vue echarts 實(shí)現(xiàn)離線中國地圖的示例代碼(細(xì)化到省份)
這篇文章主要介紹了Vue echarts 實(shí)現(xiàn)離線中國地圖,細(xì)化到省份,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09