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

axios模塊化封裝實例化及vue本地解決跨域方案

 更新時間:2023年05月31日 09:22:49   作者:灰太狼的情與殤  
這篇文章主要為大家介紹了axios模塊化封裝實例化及vue本地解決跨域示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

正文

開發(fā)環(huán)境 vue+axios+typescript+eslint+prettier

電腦系統(tǒng) windows10專業(yè)版

在使用vue開發(fā)的過程中,我們在數(shù)據(jù)交互的過程中,一般會選擇使用axios,很多小伙伴都在使用,下面我來分享一下axios封裝和vue開發(fā)解決跨域。

安裝axios

npm i axios -S

后端接口如下

主機一:http://192.168.0.103:8020
   接口: /api/login
主機二: http://192.168.0.103:3000
   接口: /chen

根目錄結(jié)構(gòu)

在根目錄下,新建文件,目錄結(jié)構(gòu)如下:

----src
----.env.development
----.env.production
----.env.test

env.development代碼

NODE_ENV = 'development'
//指定當前環(huán)境模式
VUE_APP_CURRENTMODE = 'development'
VUE_APP_BASE_URL = "http://192.168.0.103:8020"
VUE_APP_BASE_PL="http://192.168.0.103:3000"
VUE_APP_BASE_PLAPI="/chen"
VUE_APP_BASE_API="/api"

在src錄下,新建network文件夾,目錄結(jié)構(gòu)如下:

----src
-----network
------Instances(實例集合)
-------index.ts(引入實例)
-------Instance1.ts(實例1)
-------Instance2.ts(實例2)
------api.js(接口封裝)

Instances下Instance1.ts代碼

import axios from "axios";
export const Instance1 = (config: Object) => {
  const instance = axios.create({
    baseURL: process.env.VUE_APP_BASE_PLAPI,
    timeout: 900000,
  });
  instance.interceptors.request.use(
    (config) => {
      if (config.method === "get") {
        config.url = config.url + "?" + config.data;
      }
      if (sessionStorage.getItem("Authorization")) {
        config.headers.Authorization =
          "auth " + sessionStorage.getItem("Authorization");
      }
      // //console.log("請求攔截成功啦");
      // //console.log(config);
      return config;
    },
    (err) => {
      console.log("請求失敗啦");
      console.log(err);
    }
  );
  //
  instance.interceptors.response.use(
    (res) => {
      return res.data;
    },
    (err) => {
      console.log("響應失敗");
      console.log(err);
      // return err;
    }
  );
  return instance(config);
};

Instances下Instance2.ts代碼

import axios from "axios";
import qs from "qs";
export const Instance2 = (config: Object) => {
  const instance = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 900000,
  });
  instance.interceptors.request.use(
    (config) => {
      if (config.method === "get") {
        config.url = config.url + "?" + qs.stringify(config.data);
      }
      if (sessionStorage.getItem("Authorization")) {
        config.headers.Authorization =
          "auth " + sessionStorage.getItem("Authorization");
      }
      // //console.log("請求攔截成功啦");
      // //console.log(config);
      return config;
    },
    (err) => {
      console.log("請求失敗啦");
      console.log(err);
    }
  );
  //
  instance.interceptors.response.use(
    (res) => {
      return res.data;
    },
    (err) => {
      console.log("響應失敗");
      console.log(err);
      // return err;
    }
  );
  return instance(config);
};

Instances下index.ts代碼

import { Instance1 } from "@/network/Instances/Instance1";
import { Instance2 } from "@/network/Instances/Instance2";
export { Instance1, Instance2 };

networt中api.ts代碼

import { Instance1, Instance2 } from "@/network/Instances";
export const Chen = (value: any) => {
  return Instance1({
    url: "/chen",
    method: "post",
    // data: value, //傳參方式一:缺點(如果這個接口在很多地方進行了調(diào)用,修改參數(shù)了,我們需要修改很多的地方)
    data: { password, account, code } //傳參方式二:推薦傳參方法,可以更好的維護
  });
};
export const Login2 = (value: any) => {
  return Instance2({
    url: "/login",
    method: "post",
    data: value,
  });
};

在根目錄下新建vue.config.js

(和package.json同級),代碼如下

module.exports = {
  // 打包的時候去掉.map文件
  productionSourceMap: false,
  devServer: {
    proxy: {
      "/api": {
        target: process.env.VUE_APP_BASE_URL,
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/api": process.env.VUE_APP_BASE_URL + process.env.VUE_APP_BASE_API
        },
      },
      "/chen": {
        target: process.env.VUE_APP_BASE_PL,
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/chen": process.env.VUE_APP_BASE_PL
        },
      },
    }
  },
  chainWebpack: (config) => {
    // 開啟eslint自動修復,關(guān)鍵代碼
    config.module
      .rule("eslint")
      .use("eslint-loader")
      .loader("eslint-loader")
      .tap((options) => {
        options.fix = true;
        return options;
      });
  },
};

請求結(jié)果如下:

本地的分享到了這里就結(jié)束啦,希望對你有所幫助,讓我們一起努力走向巔峰,更多關(guān)于axios模塊化封裝(實例化)和vue本地解決跨域的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Vue 事件的$event參數(shù)=事件的值案例

    Vue 事件的$event參數(shù)=事件的值案例

    這篇文章主要介紹了Vue 事件的$event參數(shù)=事件的值案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Vue3 diff算法之雙端diff算法詳解

    Vue3 diff算法之雙端diff算法詳解

    雙端Diff在可以解決更多簡單Diff算法處理不了的場景,且比簡單Diff算法性能更好。本文將通過示例為大家詳細講講雙端diff算法的實現(xiàn)與使用,需要的可以參考一下
    2022-09-09
  • Vue?組件之間傳值的主要方法

    Vue?組件之間傳值的主要方法

    父組件向子組件傳值,使用 props:可以通過在子組件上綁定 props,然后在父組件中通過 v-bind 綁定相應的數(shù)據(jù)來傳遞數(shù)據(jù),這篇文章主要介紹了Vue?組件之間傳值的方法,需要的朋友可以參考下
    2023-12-12
  • 關(guān)于Element-ui中table默認選中toggleRowSelection問題

    關(guān)于Element-ui中table默認選中toggleRowSelection問題

    這篇文章主要介紹了關(guān)于Element-ui中table默認選中toggleRowSelection問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vite?vue3下配置history模式路由的步驟記錄

    vite?vue3下配置history模式路由的步驟記錄

    路由存在兩者模式,一種是歷史模式history,一種是hash模式,下面這篇文章主要給大家介紹了關(guān)于vite?vue3下配置history模式路由的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • 巧用Vue.js+Vuex制作專門收藏微信公眾號的app

    巧用Vue.js+Vuex制作專門收藏微信公眾號的app

    這篇文章主要為大家詳細介紹了vue自定義指令三步如何實現(xiàn)數(shù)據(jù)拉取更新,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 淺談vue的第一個commit分析

    淺談vue的第一個commit分析

    這篇文章主要介紹了淺談vue的第一個commit分析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • vue單向數(shù)據(jù)綁定和雙向數(shù)據(jù)綁定方式

    vue單向數(shù)據(jù)綁定和雙向數(shù)據(jù)綁定方式

    這篇文章主要介紹了vue單向數(shù)據(jù)綁定和雙向數(shù)據(jù)綁定方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 封裝 axios+promise通用請求函數(shù)操作

    封裝 axios+promise通用請求函數(shù)操作

    這篇文章主要介紹了封裝 axios+promise通用請求函數(shù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • vue如何實時往數(shù)組里追加數(shù)據(jù)

    vue如何實時往數(shù)組里追加數(shù)據(jù)

    這篇文章主要介紹了vue如何實時往數(shù)組里追加數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04

最新評論