亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

正文

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

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

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

安裝axios

npm i axios -S

后端接口如下

主機(jī)一:http://192.168.0.103:8020
   接口: /api/login
主機(jī)二: 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'
//指定當(dāng)前環(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(實(shí)例集合)
-------index.ts(引入實(shí)例)
-------Instance1.ts(實(shí)例1)
-------Instance2.ts(實(shí)例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("請(qǐng)求攔截成功啦");
      // //console.log(config);
      return config;
    },
    (err) => {
      console.log("請(qǐng)求失敗啦");
      console.log(err);
    }
  );
  //
  instance.interceptors.response.use(
    (res) => {
      return res.data;
    },
    (err) => {
      console.log("響應(yīng)失敗");
      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("請(qǐng)求攔截成功啦");
      // //console.log(config);
      return config;
    },
    (err) => {
      console.log("請(qǐng)求失敗啦");
      console.log(err);
    }
  );
  //
  instance.interceptors.response.use(
    (res) => {
      return res.data;
    },
    (err) => {
      console.log("響應(yīng)失敗");
      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ǎn)(如果這個(gè)接口在很多地方進(jìn)行了調(diào)用,修改參數(shù)了,我們需要修改很多的地方)
    data: { password, account, code } //傳參方式二:推薦傳參方法,可以更好的維護(hù)
  });
};
export const Login2 = (value: any) => {
  return Instance2({
    url: "/login",
    method: "post",
    data: value,
  });
};

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

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

module.exports = {
  // 打包的時(shí)候去掉.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自動(dòng)修復(fù),關(guān)鍵代碼
    config.module
      .rule("eslint")
      .use("eslint-loader")
      .loader("eslint-loader")
      .tap((options) => {
        options.fix = true;
        return options;
      });
  },
};

請(qǐng)求結(jié)果如下:

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

相關(guān)文章

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

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

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

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

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

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

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

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

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

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

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

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

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

    淺談vue的第一個(gè)commit分析

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

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

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

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

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

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

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

最新評(píng)論