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

vue axios 簡(jiǎn)單封裝以及思考

 更新時(shí)間:2018年10月09日 15:54:31   作者:huangenai  
axios 是一個(gè)基于Promise 用于瀏覽器和 nodejs 的 HTTP 客戶端。這篇文章主要介紹了vue axios 簡(jiǎn)單封裝以及思考 ,需要的朋友可以參考下

axios 簡(jiǎn)介

axios 是一個(gè)基于Promise 用于瀏覽器和 nodejs 的 HTTP 客戶端,它本身具有以下特征:

--------------------------------------------------------------------------------
•從瀏覽器中創(chuàng)建 XMLHttpRequest
•從 node.js 發(fā)出 http 請(qǐng)求
•支持 Promise API
•攔截請(qǐng)求和響應(yīng)
•轉(zhuǎn)換請(qǐng)求和響應(yīng)數(shù)據(jù)
•取消請(qǐng)求
•自動(dòng)轉(zhuǎn)換JSON數(shù)據(jù)
•客戶端支持防止 CSRF/XSRF

先安裝 axios

npm install axios

axios的詳細(xì)介紹以及用法 就不多說了請(qǐng) 移步 github ➡️  https://github.com/axios/axios

下面是簡(jiǎn)單的封裝一個(gè) http.js, 在此說明  checkStatus 這個(gè)方法呢 是不一定需要的 ,根據(jù)個(gè)人的項(xiàng)目需求吧,也可以直接返回response,交給后面另行處理也行。

或者根據(jù)后端返回的狀態(tài),在里面進(jìn)行處理 也行。

"use strict";
import axios from "axios";
import qs from "qs";
//添加請(qǐng)求攔截器
axios.interceptors.request.use(
 config => {
  return config;
 },
 error => {
  return Promise.reject(error);
 }
);
//添加響應(yīng)攔截器
axios.interceptors.response.use(
 response => {
  return response;
 },
 error => {
  return Promise.resolve(error.response);
 }
);
axios.defaults.baseURL = "https://www.xxxx/api";
axios.defaults.headers.post["Content-Type"] = "application/json";
axios.defaults.headers.post["X-Requested-With"] = "XMLHttpRequest";
axios.defaults.timeout = 10000;
function checkStatus(response) {
 return new Promise((resolve, reject) => {
  if (
   response &&
   (response.status === 200 ||
    response.status === 304 ||
    response.status === 400)
  ) {
   resolve(response.data);
  } else {
   reject({
    state: "0",
    message: "網(wǎng)絡(luò)異常"
   });
  }
 });
}
export default {
 post(url, params) {
  return axios({
   method: "post",
   url,
   data: params
  }).then(response => {
   return checkStatus(response);
  });
 },
 get(url, params) {
  params = qs.stringify(params);
  return axios({
   method: "get",
   url,
   params
  }).then(response => {
   return checkStatus(response);
  });
 }
};

在vue 項(xiàng)目中,main.js這個(gè)文件

import http from "./utils/http";
Vue.prototype.$http = http;

使用 helloworld.vue

...
methods: {
  async TestPost() {
   try {
    const res = await this.$http.post("/message/socketid", {
     account: "huangenai"
    });
    console.log(res);
   } catch (error) {
    console.log(error);
   }
  },
  async TestGet() {
   this.$http
    .get("/price")
    .then(res => {
     console.log(res);
    })
    .catch(error => {
     alert(error);
    });
  }
}
....

在main.js中將http.js import 進(jìn)來 并暴露到全局使用,在任何vue 頁面中 就不再需要 import http.js了,而直接通過 this.$http.post this.$http.get 來使用,在checkStatus中統(tǒng)一異步返回,順便可以處理錯(cuò)誤的情況。

個(gè)人思考:

checkStatus方法 返回了一個(gè) Promise

鏈?zhǔn)浇Y(jié)構(gòu)的話看上面那個(gè)get的方法,this.$http.get(...).then(...).catch(...),如果then 里面又來一個(gè) http請(qǐng)求 會(huì)一層包住一層。

如果使用了語法糖 async  await  ,雖然 看起來好像是簡(jiǎn)單了 不用 一層包住一層 層層嵌套,可是你必須要用到 try catch,如果出現(xiàn)異常 則直接到catch,不會(huì)再執(zhí)行下面到方法。如果再實(shí)際業(yè)務(wù)中,就算出現(xiàn)了某一個(gè)http請(qǐng)求失敗到情況,不影響下面的邏輯要繼續(xù)跑下去呢,這個(gè)就不適用了。鏈?zhǔn)浇Y(jié)構(gòu)也是 如果catch到異常 也不會(huì)執(zhí)行then 里面到方法了。

所以,是否把返回的Promise,全部都返回的是 resolve,那么 就不會(huì)說出現(xiàn)直接到了 catch 里面不執(zhí)行以下的業(yè)務(wù)了邏輯了呢。而且如果使用了語法糖 await 代碼看起來更加簡(jiǎn)潔 也不需要 try catch了, 這樣的話 reject是不是就不需要用到了呢。

function checkStatus(response) {
 return new Promise(resolve => {
  if (
   response &&
   (response.status === 200 ||
    response.status === 304 ||
    response.status === 400)
  ) {
   resolve(response.data);
  } else {
   resolve({
    state: "0",
    message: "網(wǎng)絡(luò)異常"
   });
  }
 });
}

個(gè)人覺得這兩種方案各有優(yōu)劣,實(shí)際應(yīng)用中還是應(yīng)該根據(jù)個(gè)人業(yè)務(wù)需求 業(yè)務(wù)情況而定。

相關(guān)文章

最新評(píng)論