crm开发定制Vue3中全局配置 axios 的两种方式

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

 

目录


 

 

一、回顾 crm开发定制的全局引用方式

  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 有疑问的同学可以移步  

  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/ 方式

    ① 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. };

 


结束~~~ 


 

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发