vue3如何實現(xiàn)掛載并使用axios
更新時間:2022年06月24日 11:08:40 作者:furfur-jiang
這篇文章主要介紹了vue3如何實現(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實例或組件中不用重復(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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何使用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-11
vue-calendar-component 封裝多日期選擇組件的實例代碼
這篇文章主要介紹了vue-calendar-component 封裝多日期選擇組件,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
詳解win7 cmd執(zhí)行vue不是內(nèi)部命令的解決方法
這篇文章主要介紹了詳解win7 cmd執(zhí)行vue不是內(nèi)部命令的解決方法的相關(guān)資料,這里提供了解決問題的詳細(xì)步驟,具有一定的參考價值,需要的朋友可以參考下2017-07-07

