vue3?HTTP請求中的axios示例詳解
vue3-HTTP請求
背景
vue本身不支持發(fā)送AJAX請求,需要使用vue-resource、axios等插件實(shí)現(xiàn)。
axios是一個(gè)基于Promise的HTTP請求客戶端,用來發(fā)送請求,也是vue2.0官方推薦的,同時(shí)不再對vue-resource進(jìn)行更新和維護(hù)。
axios
官網(wǎng): https://axios-http.com/
github:https://github.com/axios/axios
Axios 是一個(gè)簡單的基于 promise 的 HTTP 客戶端,適用于瀏覽器和 node.js。Axios 在具有非??蓴U(kuò)展的接口的小包中提供了一個(gè)簡單易用的庫。
安裝axios并引入
安裝:
npm的方式:
npm install axios --save
引入,【在哪里使用,就在哪里引入】
import axios from 'axios';
使用demo:
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" >{{repositoryName}}</a></div>
</div>
<!--App -->
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
repositoryUrl : '',
repositoryName : ''
}
},
mounted() {
// 發(fā)ajax請求,用以獲取數(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('請求失敗');
},
);
}
}
</script>
<style>
</style>
axios POST提交數(shù)據(jù)
Content-Type: application/json
const url = '/api/v1/web3/url/list_by_category';
let data = {"category":"tools"};
axios.post(url,data).then(
response => {
const result = response.data.data;
console.log(result)
this.repositoryName = result.name;
this.web3_urls = result
}).catch(response => {
alert('請求失敗');
},
);工作中遇到常見問題
has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource
cors阻止了你請求的資源(跨域問題)
解決方案:
在vue3.0中解決跨域首先要配置vue.config.js(在根目錄下創(chuàng)建vue.config.js、跟package.json同級的地方)
vue.config.js
在vue.config.js中加入以下代碼
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'https://www.xxx.com/', //接口域名
changeOrigin: true, //是否跨域
ws: true, //是否代理 websockets
secure: true, //是否https接口
pathRewrite: { //路徑重置
'^/api': ''
}
}
}
}
};我用的vite,參考
vue3(vite)設(shè)置代理,封裝axios,api解耦
參考URL: http://m.fzitv.net/article/271024.htm
官方:https://vitejs.dev/config/server-options.html
我們修改的是vite.config.js,內(nèi)容如下,核心就是加入了 server–> proxy字段:
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
server: {
proxy: {
'/api': {
target: 'http://127.0.0.1:8000/', //接口域名
changeOrigin: true, //是否跨域
ws: false, //是否代理 websockets
secure: false, //是否https接口
pathRewrite: { //路徑重置
'^/api': ''
}
}
}
}
})參考文獻(xiàn)
vue3(vite)設(shè)置代理,封裝axios,api解耦
參考URL: http://m.fzitv.net/article/271024.htm
到此這篇關(guān)于vue3-HTTP請求之a(chǎn)xios的文章就介紹到這了,更多相關(guān)vue3 axios請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue2.x Todo之自定義指令實(shí)現(xiàn)自動(dòng)聚焦的方法
我們希望用戶雙擊 todo 進(jìn)入編輯狀態(tài)后輸入框自動(dòng)獲取焦點(diǎn),而不是需要先手動(dòng)點(diǎn)一下。這篇文章主要介紹了Vue 2.x Todo之自定義指令實(shí)現(xiàn)自動(dòng)聚焦,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2019-01-01
手拉手教你如何處理vue項(xiàng)目中的錯(cuò)誤
在項(xiàng)目開發(fā)中經(jīng)常遇到各種報(bào)錯(cuò),每次總是通過這樣或那樣的辦法解決掉,這篇文章主要給大家介紹了關(guān)于如何處理vue項(xiàng)目中錯(cuò)誤的相關(guān)資料,需要的朋友可以參考下2022-06-06
詳談Vue.js框架下main.js,App.vue,page/index.vue之間的區(qū)別
這篇文章主要介紹了詳談Vue.js框架下main.js,App.vue,page/index.vue之間的區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
vue鼠標(biāo)懸停事件監(jiān)聽實(shí)現(xiàn)方法
頁面在鼠標(biāo)懸停(不動(dòng))n秒之后,頁面進(jìn)行相應(yīng)的事件,下面這篇文章主要給大家介紹了關(guān)于vue鼠標(biāo)懸停事件監(jiān)聽的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
vue實(shí)現(xiàn)excel文件導(dǎo)入導(dǎo)出操作示例
這篇文章主要為大家介紹了vue實(shí)現(xiàn)excel文件的導(dǎo)入導(dǎo)出實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
解決antd 下拉框 input [defaultValue] 的值的問題
這篇文章主要介紹了解決antd 下拉框 input [defaultValue] 的值的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
如何解決Element UI el-dialog打開一次后無法再次打開問題
這篇文章主要介紹了如何解決Element UI el-dialog打開一次后無法再次打開問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Vue利用Web?Speech?API實(shí)現(xiàn)文字朗讀功能
在?Vue?頁面中實(shí)現(xiàn)文字朗讀(Text-to-Speech,TTS)可以通過瀏覽器的?Web?Speech?API?實(shí)現(xiàn),下面小編就來和大家簡單講講具體實(shí)現(xiàn)步驟吧2025-02-02

