網站首頁 Vue 正文
一、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.jb51.net/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" >{{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/11656129.html
相關推薦
- 2022-05-05 基于Redis分布式BitMap的應用分析_Redis
- 2022-09-18 C++如何實現二叉樹鏈表_C 語言
- 2022-03-30 利用Python中xlwt模塊操作excel的示例詳解_python
- 2022-07-08 go語言代碼生成器code?generator使用示例介紹_Golang
- 2022-07-13 docker基本概念及安裝
- 2022-06-09 Nginx速查手冊及常見問題_nginx
- 2022-10-11 Windows下vs中對DLL、exe文件添加屬性信息
- 2022-09-04 深入了解Golang包的獲取方法_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支