vue3如何實(shí)現(xiàn)掛載并使用axios
vue3掛載并使用axios
首先在main.js中引入axios并掛載到app.config.globalProperties上
axios配置文件放置./assets/js/axios
main.js
import { createApp } from 'vue' import App from './App.vue' import './index.css' import axios from './assets/js/axios'; const app = createApp(App); app.use(router).use(ElementPlus).mount('#app') app.config.globalProperties.$http = axios;
其次配置axios.js文件
axios.js
import axios from "axios"; import qs from "qs"; import { ElMessageBox } from 'element-plus'; // axios.defaults.baseURL = '' //正式 axios.defaults.baseURL = 'http://localhost:8089' //測試 //post請求頭 axios.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8"; //設(shè)置超時 axios.defaults.timeout = 10000; axios.interceptors.request.use( config => { return config; }, error => { return Promise.reject(error); } ); axios.interceptors.response.use( response => { if (response.status == 200) { return Promise.resolve(response); } else { return Promise.reject(response); } }, error => { ElMessageBox(JSON.stringify(error), '請求異常', { confirmButtonText: '確定', callback: action => {} }); } ); export default { post(url, data) { return new Promise((resolve, reject) => { axios({ method: 'post', url, data: qs.stringify(data), }) .then(res => { resolve(res.data) }) .catch(err => { reject(err) }); }) }, get(url, data) { return new Promise((resolve, reject) => { axios({ method: 'get', url, params: data, }) .then(res => { resolve(res.data) }) .catch(err => { reject(err) }) }) } };
最后在.vue中使用
通過getCurrentInstance拿到的ctx就有了$http可以調(diào)用
import { defineComponent, getCurrentInstance, reactive, toRefs, } from "vue"; export default defineComponent({ name: "demo", props: {}, components: "", setup(props, context) { //引用全局變量 const { ctx } = getCurrentInstance(); console.log(ctx); let state = reactive({ ruleForm: { username: "fur", password: "123", } }); function submitForm() { ctx.$http.post("/login/xxx", ruleForm).then((res) => { //請求成功 }); } return { ...toRefs(state), submitForm, }; }, });
vue全局掛載axios
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div id="app"> </div> <script src="./node_modules/vue/dist/vue.js"></script> <script src="./node_modules/axios/dist/axios.js"></script> <script> // 全局掛載axios:給Vue函數(shù)添加一個原型屬性$axios指向Axios // 好處是在vue實(shí)例或組件中不用重復(fù)引用Axios,直接用this.$axios就能執(zhí)行axios方法 Vue.prototype.$axios = axios; var App = { template: ` <div><button @click="sendAjax">發(fā)請求</button></div> `, methods: { sendAjax() { console.log(this.$axios); } } }; new Vue({ el: '#app', data() { return { } }, template: ` <App /> `, components: { App } }); </script> </body> </html>
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3獲取DOM節(jié)點(diǎn)的3種方式實(shí)例
Vue本來無需操作DOM來更新界面,而且Vue也不推薦我們直接操作DOM,但是我們非要拿到DOM操作DOM怎么辦,下面這篇文章主要給大家介紹了關(guān)于Vue3獲取DOM節(jié)點(diǎn)的3種方式,需要的朋友可以參考下2023-02-02如何使用vue-json-viewer插件展示JSON格式數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于如何使用vue-json-viewer插件展示JSON格式數(shù)據(jù)的相關(guān)資料,Vue-json-viewer是一個Vue組件,用于在Vue應(yīng)用中顯示JSON數(shù)據(jù)的可視化工具,需要的朋友可以參考下2023-11-11vue-calendar-component 封裝多日期選擇組件的實(shí)例代碼
這篇文章主要介紹了vue-calendar-component 封裝多日期選擇組件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12rollup3.x+vue2打包組件的實(shí)現(xiàn)
本文主要介紹了rollup3.x+vue2打包組件的實(shí)現(xiàn),詳細(xì)的介紹了打包會存在的問題,包版本的問題,babel 轉(zhuǎn)換jsx等問題,具有一定的參考價值,感興趣的可以了解一下2023-03-03詳解win7 cmd執(zhí)行vue不是內(nèi)部命令的解決方法
這篇文章主要介紹了詳解win7 cmd執(zhí)行vue不是內(nèi)部命令的解決方法的相關(guān)資料,這里提供了解決問題的詳細(xì)步驟,具有一定的參考價值,需要的朋友可以參考下2017-07-07