• Vue3中全局配置 axios 的两种方式


    边看边学边记录系列,正好到 Vue3 了今天就和大家一起学习并记录一下 Vue3 的Composition API(组合式API) 中是如何全用使用 Axios 的!

     

    目录

    一、回顾 Vue2 的全局引用方式

      1. 简单项目的全局引用

    2. 复杂项目的三步封装

    二、Vue3 中的使用 

    1. provide/inject 方式

    2. getCurrentInstance 组合式API引入


     

     

    一、回顾 Vue2 的全局引用方式

      1. 简单项目的全局引用

        如果只是简单几个页面的使用,无需太过复杂的配置就可以直接再 main.js 中进行挂载

    1. import Vue from "vue";
    2. /* 第一步下载 axios 命令:npm i axios 或者yarn add axios 或者pnpm i axios */
    3. /* 第二步引入axios */
    4. import axios from 'axios'
    5. // 挂载一个自定义属性$http
    6. Vue.prototype.$http = axios
    7. // 全局配置axios请求根路径(axios.默认配置.请求根路径)
    8. axios.defaults.baseURL = 'http://yufei.shop:3000'

       页面使用

    1. methods:{
    2. getData(){
    3. this.$http.get('/barry').then(res=>{
    4. console.log('res',res)
    5. )}
    6. }
    7. }

    2. 复杂项目的三步封装

      ① 新建 util/request.js (配置全局的Axios,请求拦截、响应拦截等)

            关于 VFrame 有疑问的同学可以移步  前端不使用 il8n,如何优雅的实现多语言?

    1. import axios from "axios";
    2. import { Notification, MessageBox, Message } from "element-ui";
    3. import store from "@/store";
    4. import { getToken } from "@/utils/auth";
    5. import errorCode from "@/utils/errorCode";
    6. import Cookies from "js-cookie";
    7. import VFrame from "../framework/VFrame.js";
    8. import CONSTANT from '@/CONSTANT.js'
    9. axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";
    10. // 创建axios实例
    11. const service = axios.create({
    12. // axios中请求配置有baseURL选项,表示请求URL公共部分
    13. baseURL: process.env.VUE_APP_BASE_API,
    14. // 超时
    15. timeout: 120000
    16. });
    17. // request拦截器
    18. service.interceptors.request.use(
    19. config => {
    20. // 是否需要设置 token
    21. const isToken = (config.headers || {}).isToken === false;
    22. if (getToken() && !isToken) {
    23. config.headers["Authorization"] = "Bearer " + getToken(); // 让每个请求携带自定义token 请根据实际情况自行修改
    24. }
    25. var cultureName = Cookies.get(CONSTANT.UX_LANGUAGE);
    26. if (cultureName) {
    27. config.headers[CONSTANT.UX_LANGUAGE] = cultureName; // 让每个请求携带自定义token 请根据实际情况自行修改
    28. }
    29. // get请求映射params参数
    30. if (config.method === "get" && config.params) {
    31. let url = config.url + "?";
    32. for (const propName of Object.keys(config.params)) {
    33. const value = config.params[propName];
    34. var part = encodeURIComponent(propName) + "=";
    35. if (value !== null && typeof value !== "undefined") {
    36. if (typeof value === "object") {
    37. for (const key of Object.keys(value)) {
    38. let params = propName + "[" + key + "]";
    39. var subPart = encodeURIComponent(params) + "=";
    40. url += subPart + encodeURIComponent(value[key]) + "&";
    41. }
    42. } else {
    43. url += part + encodeURIComponent(value) + "&";
    44. }
    45. }
    46. }
    47. url = url.slice(0, -1);
    48. config.params = {};
    49. config.url = url;
    50. }
    51. return config;
    52. },
    53. error => {
    54. console.log(error);
    55. Promise.reject(error);
    56. }
    57. );
    58. // 响应拦截器
    59. service.interceptors.response.use(
    60. res => {
    61. // 未设置状态码则默认成功状态
    62. const code = res.data.code || 200;
    63. // 获取错误信息
    64. const msg = errorCode[code] || res.data.msg || errorCode["default"];
    65. if (code === 401) {
    66. MessageBox.alert(
    67. VFrame.l("SessionExpired"),
    68. VFrame.l("SystemInfo"),
    69. {
    70. confirmButtonText: VFrame.l("Relogin"),
    71. type: "warning"
    72. }
    73. ).then(() => {
    74. store.dispatch("LogOut").then(() => {
    75. location.href = "/index";
    76. });
    77. });
    78. } else if (code === 500) {
    79. Message({
    80. message: msg,
    81. type: "error"
    82. });
    83. if (res.data.data) {
    84. console.error(res.data.data)
    85. }
    86. return Promise.reject(new Error(msg));
    87. } else if (code !== 200) {
    88. Notification.error({
    89. title: msg
    90. });
    91. return Promise.reject("error");
    92. } else {
    93. if (res.data.uxApi) {
    94. if (res.data.success) {
    95. return res.data.result;
    96. } else {
    97. Notification.error({ title: res.data.error });
    98. console.error(res);
    99. return Promise.reject(res.data.error);
    100. }
    101. } else {
    102. return res.data;
    103. }
    104. }
    105. },
    106. error => {
    107. console.log("err" + error);
    108. let { message } = error;
    109. if (message == "Network Error") {
    110. message = VFrame.l("TheBackEndPortConnectionIsAbnormal");
    111. } else if (message.includes("timeout")) {
    112. message = VFrame.l("TheSystemInterfaceRequestTimedOut");
    113. } else if (message.includes("Request failed with status code")) {
    114. message =
    115. VFrame.l("SystemInterface") +
    116. message.substr(message.length - 3) +
    117. VFrame.l("Abnormal");
    118. }
    119. Message({
    120. message: VFrame.l(message),
    121. type: "error",
    122. duration: 5 * 1000
    123. });
    124. return Promise.reject(error);
    125. }
    126. );
    127. export default service;

      ② 新建 api/login.js (配置页面所需使用的 api)

    1. import request from '@/utils/request'
    2. // 登录方法
    3. export function login(username, password,shopOrgId,counter, code, uuid) {
    4. const data = {
    5. username,
    6. password,
    7. shopOrgId,
    8. counter,
    9. uuid
    10. }
    11. return request({
    12. url: '/login',
    13. method: 'post',
    14. data: data
    15. })
    16. }
    17. // 获取用户详细信息
    18. export function getInfo() {
    19. return request({
    20. url: '/getInfo',
    21. method: 'get'
    22. })
    23. }
    24. // 退出方法
    25. export function logout() {
    26. return request({
    27. url: '/logout',
    28. method: 'post'
    29. })
    30. }

      ③ 页面使用引入

    1. import { login } from "@/api/login.js"
    2. 接下来不用多说,相信大家已经会使用了

    二、Vue3 中的使用 

     上面回顾完 Vue2 中使用 axios 我们来一起看看 Vue3 中axios的使用( 简单Demo,前台使用Vue3,后台使用 node.js ),仅供学习!

    1. provide/inject 方式

        ① main.js 中 使用 provide 传入

    1. import {
    2. createApp
    3. } from 'vue'
    4. import App from './App.vue'
    5. import router from './router'
    6. import store from './store'
    7. import "lib-flexible/flexible.js"
    8. import axios from "@/util/request.js"
    9. const app = createApp(App);
    10. app.provide('$axios', axios)
    11. app.use(store).use(router).mount('#app');

        ② 需要用到的页面使用 inject 接受

    1. import { ref, reactive, inject, onMounted} from "vue";
    2. export default {
    3. setup() {
    4. const $axios = inject("$axios");
    5. const getData = async () => {
    6. data = await $axios({ url: "/one/data" });
    7. console.log("data", data);
    8. };
    9. onMounted(() => {
    10. getData()
    11. })
    12. return { getData }
    13. }
    14. }

    这个就是借助 provide 做一个派发,和 Vue2 中的差距使用方法差距不大 

     

    2. getCurrentInstance 组合式API引入

     ① main.js 中挂载

    1. import {
    2. createApp
    3. } from 'vue'
    4. import App from './App.vue'
    5. import router from './router'
    6. import store from './store'
    7. import "lib-flexible/flexible.js"
    8. import axios from "@/util/request.js"
    9. const app = createApp(App);
    10. /* 挂载全局对象 */
    11. app.config.globalProperties.$axios = axios;
    12. app.use(store).use(router).mount('#app');

    /* 挂载全局对象 */
    app.config.globalProperties.$axios = axios;

    重点就是上面这句

    ② 需要用的页面使用 Composition Api -- getCurrentInstance 拿到

    1. <script>
    2. import { reactive, onMounted, getCurrentInstance } from "vue";
    3. export default {
    4. setup() {
    5. let data = reactive([]);
    6. /**
    7. * 1. 通过getCurrentInstance方法获取当前实例
    8. * 再根据当前实例找到全局实例对象appContext,进而拿到全局实例的config.globalProperties。
    9. */
    10. const currentInstance = getCurrentInstance();
    11. const { $axios } = currentInstance.appContext.config.globalProperties;
    12. /**
    13. * 2. 通过getCurrentInstance方法获取上下文,这里的proxy就相当于this。
    14. */
    15. const { proxy } = currentInstance;
    16. const getData = async () => {
    17. data = await $axios({ url: "/one/data" });
    18. console.log("data", data);
    19. };
    20. const getData2 = async () => {
    21. data = await proxy.$axios({ url: "/one/data" });
    22. console.log("data2", data);
    23. };
    24. onMounted(() => {
    25. getData()
    26. });
    27. return { getData };
    28. },
    29. };
    30. </script>

    下图可以看到我们确实调用了 2次 API 

    其实通过 Composition API 中的 getCurrentInstance 方法也是有两种方式的

     1. 通过 getCurrentInstance 方法获取当前实例,再根据当前实例找到全局实例对象appContext,进而拿到全局实例的config.globalProperties。        

    1. const currentInstance = getCurrentInstance();
    2. const { $axios } = currentInstance.appContext.config.globalProperties;

     2. 通过getCurrentInstance方法获取上下文,这里的proxy就相当于this。

    1. const currentInstance = getCurrentInstance();
    2. const { proxy } = currentInstance;
    3. const getData2 = async () => {
    4. data = await proxy.$axios({ url: "/one/data" });
    5. console.log("data2", data);
    6. };

     


    结束~~~ 


     

  • 相关阅读:
    Promise.all 使用方法
    谣言检测论文精度——1.Detecting Rumors from Microblogs with Recurrent Neural Networks
    java计算机毕业设计汽车技术资料管理系统源码+系统+数据库+lw文档+mybatis+运行部署
    Nginx——下载,安装,启动,关闭,配置——负载均衡——静态代理——动静分离
    关于Go中两个模块互相调用的场景解决方案
    leetcode Top100(17)矩阵置零
    matlab 坡度滤波算法地面分割
    MySQL表的增删改查--你都知道吗?
    C#编程学习
    webpack5入门配置
  • 原文地址:https://blog.csdn.net/weixin_56650035/article/details/125610295