最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于axios在vue中的使用

 更新時間:2022年03月29日 14:21:31   作者:前端報刊  
這篇文章主要介紹了關(guān)于axios在vue中的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

一、Axios是什么

  • Axios是一個基于promise的HTTP庫(類似于jQuery的Ajax,用于HTTP請求)
  • 可以用于瀏覽器和node.js(既可以用于客戶端也可以用于node.js編寫的服務端)

二、Axios有哪些特性

  • 支持promise API
  • 攔截請求和響應
  • 轉(zhuǎn)換請求數(shù)據(jù)和響應數(shù)據(jù)
  • 取消請求
  • 自動轉(zhuǎn)換 JSON 數(shù)據(jù)
  • 客戶端支持防御 XSRF

三、Axios瀏覽器支持

四、安裝

1.使用 npm

$ npm install axios

2.使用 bower

$ bower install axios

3.使用 cdn

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

五、用法(vue項目已搭建)

1. Axios基礎(chǔ)用法

(get、post、put 等請求方法)

先介紹一下,Axios常用的幾種請求方法有哪些:get、post、put、patch、delete

  • get:(一般用于)獲取數(shù)據(jù)
  • post:提交數(shù)據(jù)(表單提交+文件上傳)
  • put:更新(或編輯)數(shù)據(jù)(所有數(shù)據(jù)推送到后端(或服務端))
  • patch:更新數(shù)據(jù)(只將修改的數(shù)據(jù)推送到后端)
  • delete:刪除數(shù)據(jù)

題外話,一般公司在實際開發(fā)項目過程中:

(1)post:一般用于新建

(2)put:一般用于更新(適合數(shù)據(jù)量比較少的更新)

(3)patch:一般用于數(shù)據(jù)量比較大;假如一個表單的數(shù)據(jù)量很大,有很多項,使用put的話,全部數(shù)據(jù)推送一次是比較耗性能的,這個時候可以考慮用patch,只將修改的數(shù)據(jù)推送到后端

以上這些題外話,只是一般的常規(guī)用法,不能代表一定要這樣用;當然了,你可能會說我用post來獲取數(shù)據(jù),行不行?這個當然行了,絕對沒問題!具體怎么用還是前后端一起商量著決定,以免發(fā)生不愉快的肢體沖突!

哈哈哈,嚴重了啊,開個玩笑!

 (1)get 請求

<template>
? <div>
? ? <div>mmmm</div>
? </div>
</template>
?
<script>
import axios from 'axios'
?
// get 請求
export default {
? name: "get",
? created() {
?
? ? //第一種寫法叫做get別名請求方法
? ? //http://localhost:8080/static/data.json?id=1
? ? axios.get('../../static/data.json', {
? ? ? params: {//有參數(shù)時,若無參數(shù)時直接省略不寫
? ? ? ? id: 1
? ? ? }
? ? }).then((res) => {
? ? ? console.log('數(shù)據(jù):', res);
? ? })
?
? ? //第二種寫法
? ? axios({
? ? ? method: 'get',
? ? ? url: '../../static/data.json',
? ? ? params: {
? ? ? ? id: 1
? ? ? }
? ? }).then((res) => {
? ? ? console.log('數(shù)據(jù):', res)
? ? })
? }
}
</script>
?
<style scoped>
?
</style>

下面了解一下請求信息--- 

Status Code:304 Not Modified---304是重定向;正常情況下,第一次訪問接口的時候返回的 都是200;當你第二次訪問接口的時候,如果數(shù)據(jù)沒有變化, 那么瀏覽器會自動識別返回一個狀態(tài)304,代表數(shù)據(jù)沒有更改 、重定向;相當于重定向到你剛剛訪問的資源,這樣的話會加載 更快! 

(2) post 請求

<template>
  <div>
    <div>mmmm</div>
  </div>
</template>
 
<script>
import axios from 'axios'
 
// post 請求
export default {
  name: "post",
  created() {
 
    /*
    post常用的請求數(shù)據(jù)(data)格式有兩種:
    (1)applicition/json
    (2)form-data 表單提交(圖片上傳,文件上傳)
     */
 
    //第一種寫法叫做post別名請求方法
    // http://localhost:8080/static/data.json?id=1
    // applicition/json 請求
    let data = {
      id: 1
    }
    axios.post('../../static/data.json', data)
      .then((res) => {
        console.log('數(shù)據(jù):', res);
      })
    //第二種寫法
    axios({
      method: 'post',
      url: '../../static/data.json',
      data: data,
    }).then((res) => {
      console.log('數(shù)據(jù):', res)
    })
    // form-data 請求
    let formData = new FormData()
    for (let key in data) {
      formData.append(key, data[key])
    }
    axios.post('../../static/data.json', formData)
      .then((res) => {
        console.log('數(shù)據(jù):', res);
      })
  }
}
</script>
 
<style scoped>
 
</style>

了解一下,post兩種請求方式的Content-Type和參數(shù)有哪些不同---

applicition/json:如下圖

form-data:如下圖

(3)put、patch 請求

說明一下,put和patch請求與post請求用法一樣類似,同樣有applicition/json和form-data,為了節(jié)省時間就不過多贅述了,簡單寫一下!

<template>
  <div>
    <div>mmmm</div>
  </div>
</template>
 
<script>
import axios from 'axios'
 
// put、patch 請求
export default {
  name: "put,patch",
  created() {
 
    let data = {
      id: 1
    }
    // put 請求
    axios.put('../../static/data.json', data)
      .then((res) => {
        console.log('數(shù)據(jù):', res);
      })
    // patch 請求
    axios.patch('../../static/data.json', data)
      .then((res) => {
        console.log('數(shù)據(jù):', res);
      })
  }
}
</script>
 
<style scoped>
 
</style>

(4)delete 請求

delete請求與前四種請求稍有一點不同:delete請求有時候需要把參數(shù)拼接到URL上,有時候像post請求那樣把參數(shù)放在請求體里面。至于具體怎么調(diào)用,需要和后端商量好!

<template>
  <div>
    <div>mmmm</div>
  </div>
</template>
 
<script>
import axios from 'axios'
 
// delete 請求
export default {
  name: "delete",
  created() {
 
    // delete 請求
    axios.delete('../../static/data.json', {
      params: {// 把參數(shù)拼接到URL上
        id: 1
      }
    })
      .then((res) => {
        console.log('數(shù)據(jù):', res);
      })
 
    axios.delete('../../static/data.json', {
      data: {// 把params改成data,把參數(shù)放在請求體里面
        id: 1
      }
    })
      .then((res) => {
        console.log('數(shù)據(jù):', res);
      })
    // 不用別名方法
    axios({
      method: 'delete',
      url: '../../static/data.json',
      //params: {id: 1},// 把參數(shù)拼接到URL上
      data: {id: 1}// 把參數(shù)放在請求體里面
    }).then((res) => {
      console.log('數(shù)據(jù):', res)
    })
  }
 
}
</script>
 
<style scoped>
 
</style>

了解一下,把參數(shù)拼接到URL上和放在請求體里面有什么不同---

params(把參數(shù)拼接到URL上),如下圖:

data(把參數(shù)放在請求體里面),如下圖:

(5)并發(fā)請求

并發(fā)請求:同時進行多個請求,并統(tǒng)一處理返回值。

<template>
  <div>
    <div>mmmm</div>
  </div>
</template>
 
<script>
import axios from 'axios'
 
// 并發(fā)請求
export default {
  name: "get",
  created() {
 
    // 并發(fā)請求用到了axios的兩個方法:axios.all('參數(shù)是一個數(shù)組')、axios.spread('回調(diào)函數(shù)')
    axios.all([
      axios.get('../../static/data.json'),
      axios.get('../../static/city.json')
    ]).then(axios.spread((dataRes, cityRes) => {
      console.log(dataRes, cityRes)
    }))
  }
}
</script>
 
<style scoped>
 
</style>

2. Axios進階用法

(實例、配置、攔截器、取消請求等)

(1)axios實例和配置

<template>
  <div>
    <div>mmmm</div>
  </div>
</template>
 
<script>
import axios from 'axios'
// axios 實例
// 后端接口地址有多個,并且超時時長不一樣
export default {
  name: "get",
  created() {
    //創(chuàng)建axios實例
    let instance = axios.create({//參數(shù)配置
      baseURL: 'http://localhost:8080',//請求的域名(或基本地址)
      timeout: 3000,//請求的超時時長(默認:1000毫秒(ms),超過這個時長會報401超時)
      //url: '../../static/data.json',//請求的路徑
      //method: 'get',//請求方法
      headers: {//設(shè)置請求頭(給請求頭添加一些參數(shù))
        token: ''
      },
      //params: {id: 1},//請求參數(shù)拼接在URL上
      //data: {id: 1}//請求參數(shù)放在請求體
    })
    instance.get('../../static/data.json', {
      params: {
        id: 1
      }
    })
      .then((res) => {
        console.log(res)
      })
    //假如有兩個域名或設(shè)置超時時長不一樣,你可以再創(chuàng)建一個axios實例
    let instance2 = axios.create({
      baseURL: 'http://localhost:9090',
      timeout: 5000
    })
    instance2.get('../../static/data.json', {
      params: {
        id: 1
      }
    })
      .then((res) => {
        console.log(res)
      })
 
    //1.axios全局配置
    axios.defaults.baseURL = 'http://localhost:8080';
    axios.defaults.timeout = 3000;
    //2.axios實例配置
    let instance = axios.create();
    instance.defaults.timeout = 4000;
    //3.axios請求配置
    instance.get('../../static/data.json', {
      timeout: 6000
    })
      .then((res) => {
        console.log(res)
      })
    //優(yōu)先級從低到高:1.axios全局配置 < 2.axios實例配置 < 3.axios請求配置
  }
}
</script>
 
<style scoped>
 
</style>

(2)攔截器

<template>
  <div>
    <div>mmmm</div>
  </div>
</template>
 
<script>
import axios from 'axios'
// 攔截器:在請求或響應被處理前攔截它們
// 請求攔截器、響應攔截器
export default {
  name: "get",
  created() {
    //請求攔截器
    axios.interceptors.request.use(config => {
      //在發(fā)送請求前做些什么
      return config;
    }, err => {
      //在請求錯誤的時候做些什么
      return Promise.reject(err);
    })
 
    //響應攔截器
    axios.interceptors.response.use(res => {
      //請求成功對響應數(shù)據(jù)做處理
      return res;
    }, err => {
      //響應錯誤做些什么
      return Promise.reject(err);
    })
 
    //取消攔截器(了解)
    let interceptors = axios.interceptors.request.use(config => {
      config.headers = {
        auth: true
      }
      return config;
    })
    axios.interceptors.request.eject(interceptors);
 
    //例子:登錄狀態(tài)(token:'') 需要登錄的接口
    let instance = axios.create({});
    instance.interceptors.request.use(config => {
      config.headers.token = '';
      // config.headers = {//這種寫法會覆蓋掉headers中的其他參數(shù),導致headers中只包含token這一個參數(shù),所以不建議這種寫法
      //   token: ''
      // }
      return config;
    }, err => {
      return Promise.reject(err);
    })
 
    //移動端彈窗
    let instance_phone = axios.create({});
    instance_phone.interceptors.request.use(config => {
      $('#modal').show();
      return config;
    })
    instance_phone.interceptors.response.use(res => {
      $('#modal').hide();
      return res;
    })
  }
}
</script>
 
<style scoped>
 
</style>

3.Axios進一步封裝

在項目中的實際應用

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

在一個完整的項目中,和服務端的交互會很頻繁,一個項目會有很多請求,冗余代碼很多。所以將請求封裝,統(tǒng)一管理還是很有必要的。

本文介紹的axios的封裝主要目的就是在幫助我們簡化項目代碼和利于后期的更新維護。 

(1)第一步:src/api/request.js

import axios from 'axios'
// import Vue from 'vue';
// import store from '../store';
// import {router} from '../router/index';
 
// let vm = new Vue();
 
const instance = axios.create({
  baseURL: 'http://localhost:8080',
  timeout: 3000,
  // headers: {
  //   post: {
  //     'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  //   }
  // }
})
 
// 請求攔截
instance.interceptors.request.use(config => {
  // 自定義header,可添加項目token
  // if (store.state.app.token) {
  //   config.headers.token = store.state.app.token;
  //   config.headers.timestamp = new Date().getTime();
  // }
  return config;
}, error => {
  return Promise.reject(error);
})
 
// 響應攔截
instance.interceptors.response.use(response => {
  // const resCode = response.status;
  // if (resCode === 200) {
  //   return Promise.resolve(response);
  // } else {
  //   return Promise.reject(response);
  // }
  return response;
}, error => {
  // const resCode = error.response.status;
  // switch (resCode) {
  //   case 401:
  //     vm.$Message.error(error.response.data.message);
  //     store.commit('logout', this);
  //     store.commit('clearOpenedSubmenu');
  //     // console.log('token-0', store.state.app.token);
  //     router.replace({
  //       name: 'login'
  //     });
  //     break;
  //   case 404:
  //     vm.$Message.error('網(wǎng)絡(luò)請求不存在');
  //     break;
  //   case 500:
  //     vm.$Message.error('服務器連接錯誤');
  //     break;
  //   // 其他狀態(tài)碼錯誤提示
  //   default:
  //     vm.$Message.error(error.response.data.message);
  // }
  return Promise.reject(error);
})
 
/*
 *封裝get方法
 *@param{String} url [請求地址]
 *@param{Object} params 請求參數(shù)
 */
export function Get(url, params) {
  return new Promise((resolve, reject) => {
    instance.get(url, {
      params: params
    }).then((res) => {
      resolve(res.data);
    }).catch((error) => {
      reject(error.data);
    })
  })
}
 
/**
 *封裝post方法
 *@param{String} url 請求地址
 *@param{Object} params 請求參數(shù)
 */
export function Post(url, params) {
  return new Promise((resolve, reject) => {
    instance.post(url, params).then((res) => {
      resolve(res.data);
    }).catch((error) => {
      reject(error.data);
    })
  })
}
 
/**
 *封裝put方法
 *@param{String} url 請求地址
 *@param{Object} params 請求參數(shù)
 */
export function Put(url, params) {
  return new Promise((resolve, reject) => {
    instance.put(url, params).then((res) => {
      resolve(res.data);
    }).catch((error) => {
      reject(error.data);
    })
  })
}
 
/**
 *封裝patch方法
 *@param{String} url 請求地址
 *@param{Object} params 請求參數(shù)
 */
export function Patch(url, params) {
  return new Promise((resolve, reject) => {
    instance.put(url, params).then((res) => {
      resolve(res.data);
    }).catch((error) => {
      reject(error.data);
    })
  })
}
 
/**
 *封裝delete方法
 *@param{String} url [請求地址]
 *@param{Object} params [請求參數(shù)]
 */
export function Delete(url, params) {
  return new Promise((resolve, reject) => {
    instance.delete(url, {
      params: params
    }).then((res) => {
      resolve(res.data);
    }).catch((error) => {
      reject(error.data);
    })
  })
}

(2)第二步:src/api/index.js

import {Get,Post,Put,Patch,Delete} from "@/api/request";
 
export default {
  getListData: (params) => {
    return Get('../../static/data.json',params);
  },
  postListData: (params) => {
    return Post('../../static/data.json',params);
  },
  deleteListData: (params) => {
    return Delete('../../static/data.json',params);
  }
}
 

(3)第三步:src/main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/store'
import Api from './api/index';
 
Vue.config.productionTip = false
Vue.prototype.$axios = Api;
 
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

(4)第四步:src/components/HelloWorld.vue

<template>
 
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data() {
    return {}
  },
  methods: {
    getData() {
      let data = {
        id: 1
      }
      this.$axios.getListData(data)
        .then((res) => {
          console.log(res);
        })
    },
    postData() {
      let data = {
        id: 1,
        msg: 2
      }
      this.$axios.postListData(data)
        .then((res) => {
          console.log(res);
        })
    },
    postFormData() {
      let data = {
        id: 1,
        msg: 2
      }
      let formData = new FormData();
      for (let key in data) {
        formData.append(key, data[key]);
      }
      this.$axios.postListData(formData)
        .then((res) => {
          console.log(res);
        })
    },
    deleteData() {
      let data = {
        id: 1
      }
      this.$axios.deleteListData(data)
        .then((res) => {
          console.log(res);
        })
    },
  },
  created() {
    this.getData();
    this.postData();
    this.postFormData();
    this.deleteData();
  }
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 
</style>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue:Bin Code Editor格式化JSON編輯器詳解

    Vue:Bin Code Editor格式化JSON編輯器詳解

    文章介紹了BinCodeEditor組件的安裝、注冊、使用方法以及注意事項,組件可以通過npm或yarn安裝,支持全局或局部注冊,使用時,可以通過value屬性綁定JavaScript值,或使用v-model,組件事件與方法包括編輯區(qū)顯示問題的解決
    2024-12-12
  • Vue3播放m3u8視頻的兩種實現(xiàn)方式示例

    Vue3播放m3u8視頻的兩種實現(xiàn)方式示例

    這篇文章主要介紹了Vue3播放m3u8視頻的兩種實現(xiàn)方式,兩種視頻播放器組件分別是vue3-video-play和chimee-player,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-01-01
  • vue.js修改el-popover組件顯示位置

    vue.js修改el-popover組件顯示位置

    el-popover是一個基于element-ui框架設(shè)計的,用于浮動展示提示或菜單的UI組件,下面這篇文章主要給大家介紹了關(guān)于vue.js修改el-popover組件顯示位置的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • vue2.0 datepicker使用方法

    vue2.0 datepicker使用方法

    這篇文章主要介紹了vue2.0 datepicker的使用方法,非常不錯,具有參考借鑒借鑒價值,需要的朋友參考下
    2018-02-02
  • vue如何在新窗口打開頁面

    vue如何在新窗口打開頁面

    這篇文章主要介紹了vue如何在新窗口打開頁面問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • vue下axios攔截器token刷新機制的實例代碼

    vue下axios攔截器token刷新機制的實例代碼

    這篇文章主要介紹了vue下axios攔截器token刷新機制的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01
  • Vue登錄后添加動態(tài)路由并跳轉(zhuǎn)的實踐分享

    Vue登錄后添加動態(tài)路由并跳轉(zhuǎn)的實踐分享

    這篇文章講給大家詳細介紹一下Vue如何實現(xiàn)登錄后添加動態(tài)路由并跳轉(zhuǎn),文章通過代碼示例介紹的非常詳細,對我們的學習或工作有一定的的幫助,需要的朋友可以參考下
    2023-07-07
  • 從0到1搭建Element的后臺框架的方法步驟

    從0到1搭建Element的后臺框架的方法步驟

    這篇文章主要介紹了從0到1搭建Element的后臺框架的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • vue項目引入字體.ttf的方法

    vue項目引入字體.ttf的方法

    今天小編就為大家分享一篇vue項目引入字體.ttf的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • vuejs簡單驗證碼功能完整示例

    vuejs簡單驗證碼功能完整示例

    這篇文章主要介紹了vuejs簡單驗證碼功能,結(jié)合完整實例形式分析了vue.js驗證碼的生成、顯示、校驗等相關(guān)操作技巧,需要的朋友可以參考下
    2019-01-01

最新評論

渭源县| 罗甸县| 镇康县| 葫芦岛市| 太仆寺旗| 县级市| 德安县| 青海省| 房山区| 崇信县| 淄博市| 平利县| 大理市| 安图县| 十堰市| 枣强县| 和静县| 兖州市| 武宁县| 藁城市| 南郑县| 佛教| 屯昌县| 济阳县| 乾安县| 中卫市| 象州县| 磐石市| 工布江达县| 德州市| 梨树县| 全南县| 宁国市| 开化县| 大方县| 桐梓县| 衡水市| 衡阳市| 华池县| 县级市| 延吉市|