vue3?HTTP請求中的axios示例詳解
vue3-HTTP請求
背景
vue本身不支持發(fā)送AJAX請求,需要使用vue-resource、axios等插件實現(xiàn)。
axios是一個基于Promise的HTTP請求客戶端,用來發(fā)送請求,也是vue2.0官方推薦的,同時不再對vue-resource進行更新和維護。
axios
官網(wǎng): https://axios-http.com/
github:https://github.com/axios/axios
Axios 是一個簡單的基于 promise 的 HTTP 客戶端,適用于瀏覽器和 node.js。Axios 在具有非??蓴U展的接口的小包中提供了一個簡單易用的庫。
安裝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ù)最高的項目 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://chabaoo.cn/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': '' } } } } })
參考文獻
vue3(vite)設(shè)置代理,封裝axios,api解耦
參考URL: http://chabaoo.cn/article/271024.htm
到此這篇關(guān)于vue3-HTTP請求之a(chǎn)xios的文章就介紹到這了,更多相關(guān)vue3 axios請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue移動端下拉加載更多數(shù)據(jù)onload實現(xiàn)方法淺析
這篇文章主要介紹了Vue移動端下拉加載更多數(shù)據(jù)onload實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02vue基于element-china-area-data插件實現(xiàn)省市區(qū)聯(lián)動
省市區(qū)聯(lián)動在日常開發(fā)中用的非常多,本文就介紹一下vue基于element-china-area-data插件實現(xiàn)省市區(qū)聯(lián)動,具有一定的參考價值,感興趣的可以了解一下2022-04-04Vue 配合eiement動態(tài)路由,權(quán)限驗證的方法
今天小編就為大家分享一篇Vue 配合eiement動態(tài)路由,權(quán)限驗證的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09