<strike id="cy2gs"><menu id="cy2gs"></menu></strike>
  • <del id="cy2gs"><dfn id="cy2gs"></dfn></del>
  • Vue 中 axios 攔截器的封裝

    2019-12-4    seo達(dá)人

    在vue項目中,和后臺交互獲取數(shù)據(jù)這塊,我們通常使用的是axios庫,它是基于promise的http庫,可運(yùn)行在瀏覽器端和node.js中。他有很多優(yōu)秀的特性,例如攔截請求和響應(yīng)、取消請求、轉(zhuǎn)換json、客戶端防御cSRF等。

    一 . 安裝



    npm install axios;

    1

    二 . 引入

    在項目的src目錄中,新建一個request文件夾,然后在里面新建一個http.js和一個api.js文件。http.js文件用來封裝我們的axios,api.js用來統(tǒng)一管理我們的接口。

    三 . 開始封裝



    在http.js中引入axios . vue及其他

    import Axios from 'axios'; // 引入axios

    import { Message, Loading, Notification } from 'element-ui'

    import vue from 'vue';

    1

    2

    3

    http.js文件全部代碼如下:



    import Axios from 'axios';

    import store from '../store';

    import { Message, Loading, Notification } from 'element-ui'

    import vue from 'vue';

    // 環(huán)境的切換

    if (process.env.NODE_ENV == 'development') {

      Axios.defaults.baseURL = "http://10.230.39.58:33390/devops";

    }

    else if (process.env.NODE_ENV == 'production') {

      Axios.defaults.baseURL = "http://10.230.39.58:33390/devops";

    }

    // request請求攔截器

    Axios.defaults.withCredentials = true

    vue.prototype.$axios = Axios

    //請求超時時間

    // Axios.defaults.timeout = 100000;

    Axios.defaults.headers.get['Content-Type'] = "application/json"

    Axios.interceptors.request.use(config => {

      const Basic = sessionStorage.getItem("basicParam")

      if (Basic) {

        config.headers.Authorization = Basic ${Basic};

      } else {

        console.log("無校驗值");

      }

      return config;

    }, error => {

      Promise.reject(error);

    })

    // respone返回攔截器

    Axios.interceptors.response.use(

      response => {

        if (response.data.code !== 200) {

          Notification.error({

            title: '錯誤',

            message: response.data.message

          });

        }

        return response.data;

      }, error => {

        // Notification.error({

        //   title: '錯誤',

        //   message: '系統(tǒng)異常'

        // });

        console.log('err' + error);// for debug

        return Promise.reject(error);

      }

    )



    export default Axios;



    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    api.js文件全部代碼如下:



    import axios from 'axios';

    /

      封裝get方法

     
    @param url

      @param data

     
    @returns {Promise}

     */



    export function fetch(url, params = {}) {

      return new Promise((resolve, reject) => {

        axios.get(url, {

          params: params

        })

          .then(response => {

            resolve(response.data);

          })

          .catch(err => {

            reject(err)

          })

      })

    }





    /*

     
    封裝post請求

      @param url

     
    @param data

      @returns {Promise}

     
    /



    export function _post(url, data = {}) {

      return new Promise((resolve, reject) => {

        axios.post(url, data)

          .then(response => {

            console.log(response,

              "response");



            resolve(response);

          }, err => {

            reject(err)

          })

      })

    }



    /

    • 封裝patch請求
    • @param url
    • @param data
    • @returns {Promise}

      */



      export function patch(url, data = {}) {

        return new Promise((resolve, reject) => {

          axios.patch(url, data)

            .then(response => {

              resolve(response.data);

            }, err => {

              reject(err)

            })

        })

      }



      /**
    • 封裝put請求
    • @param url
    • @param data
    • @returns {Promise}

      */



      export function put(url, data = {}) {

        return new Promise((resolve, reject) => {

          axios.put(url, data)

            .then(response => {

              resolve(response.data);

            }, err => {

              reject(err)

            })

        })

      }



      在main.js中引入api.js



      import { _post, fetch, patch, put } from './utils/api'

      //定義全局變量

      Vue.prototype.$post = post;

      Vue.prototype.$fetch = fetch;

      Vue.prototype.$patch = patch;

      Vue.prototype.$put = put;

      1

      2

      3

      4

      5

      6

      最后在組件里直接使用



       this.$post('/api/v2/movie/top250')

            .then((response) => {

              console.log(response)

            })

            



      其他方法用法相同




    日歷

    鏈接

    個人資料

    存檔

    主站蜘蛛池模板: 国产精品美女WWW爽爽爽视频| 久久国产精品视频| 97精品伊人久久久大香线蕉| 精品国产网红福利在线观看| 久久精品国产精品亚洲人人| 97在线精品视频| 日韩AV无码精品人妻系列| 精品一区二区三区免费观看| 国产精品久久久久9999| 精品无人区一区二区三区| 国产精品人人爽人人做我的可爱| 69久久夜色精品国产69 | 中文字幕精品亚洲无线码一区| 亚洲av无码国产精品色午夜字幕 | 国产成人精品免费视频大全| 久热精品视频第一页| 欧美精品在线一区二区三区| 国内精品人妻无码久久久影院| 国产亚洲婷婷香蕉久久精品| 精品综合久久久久久888蜜芽| 中文字幕无码精品亚洲资源网久久| 精品无码人妻久久久久久| 伊人久久大香线蕉精品| 成人午夜精品网站在线观看| 国产精品兄妹在线观看麻豆 | 久久亚洲精品中文字幕| 在线精品自拍无码| 亚洲欧美日韩国产一区二区三区精品| 精品少妇一区二区三区在线| 精品国产一区二区三区AV性色| 国产精品午夜一级毛片密呀| 国产精品手机在线观看你懂的| 北条麻妃国产九九九精品视频| 亚洲无删减国产精品一区| 久久亚洲国产精品一区二区| 欧美精品国产日韩综合在线| 亚洲成人精品久久| 国产99精品一区二区三区免费| 国产精品极品| 久久精品无码av| 人妻少妇精品系列|