欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3如何實(shí)現(xiàn)掛載并使用axios

 更新時間:2022年06月24日 11:08:40   作者:furfur-jiang  
這篇文章主要介紹了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í)例

    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使用csp的簡單示例

    vue使用csp的簡單示例

    Vue是一套用于構(gòu)建用戶界面的漸進(jìn)式框架,與其它大型框架不同的是,Vue被設(shè)計為可以自底向上逐層應(yīng)用,下面這篇文章主要給大家介紹了關(guān)于vue使用csp的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • 如何使用vue-json-viewer插件展示JSON格式數(shù)據(jù)

    如何使用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 封裝多日期選擇組件的實(shí)例代碼

    vue-calendar-component 封裝多日期選擇組件的實(shí)例代碼

    這篇文章主要介紹了vue-calendar-component 封裝多日期選擇組件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • 解決ant-design-vue安裝報錯的問題

    解決ant-design-vue安裝報錯的問題

    這篇文章主要介紹了解決ant-design-vue安裝報錯的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • vue實(shí)現(xiàn)百分比占比條效果

    vue實(shí)現(xiàn)百分比占比條效果

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)百分比占比條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • rollup3.x+vue2打包組件的實(shí)現(xiàn)

    rollup3.x+vue2打包組件的實(shí)現(xiàn)

    本文主要介紹了rollup3.x+vue2打包組件的實(shí)現(xiàn),詳細(xì)的介紹了打包會存在的問題,包版本的問題,babel 轉(zhuǎn)換jsx等問題,具有一定的參考價值,感興趣的可以了解一下
    2023-03-03
  • vue+openlayers繪制省市邊界線

    vue+openlayers繪制省市邊界線

    這篇文章主要為大家詳細(xì)介紹了vue+openlayers繪制省市邊界線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • Vue3使用contenteditable打造定制化輸入

    Vue3使用contenteditable打造定制化輸入

    contenteditable 屬性為網(wǎng)頁開發(fā)者提供了一種靈活的方式來創(chuàng)建可編輯的內(nèi)容區(qū)域,使用戶可以直接在網(wǎng)頁上進(jìn)行內(nèi)容編輯,而無需依賴傳統(tǒng)的輸入框,本文將利用contenteditable打造定制化輸入,感興趣的可以了解下
    2023-12-12
  • 詳解win7 cmd執(zhí)行vue不是內(nèi)部命令的解決方法

    詳解win7 cmd執(zhí)行vue不是內(nèi)部命令的解決方法

    這篇文章主要介紹了詳解win7 cmd執(zhí)行vue不是內(nèi)部命令的解決方法的相關(guān)資料,這里提供了解決問題的詳細(xì)步驟,具有一定的參考價值,需要的朋友可以參考下
    2017-07-07

最新評論