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

Vue axios全局?jǐn)r截 get請(qǐng)求、post請(qǐng)求、配置請(qǐng)求的實(shí)例代碼

 更新時(shí)間:2018年11月28日 12:56:50   作者:蝸牛速度額  
這篇文章主要介紹了Vue axios全局?jǐn)r截 get請(qǐng)求、post請(qǐng)求、配置請(qǐng)求的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

下面通過(guò)一段代碼給大家介紹Vue axios全局?jǐn)r截 get請(qǐng)求、post請(qǐng)求、配置請(qǐng)求,具體代碼如下所述:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <script src="../node_modules/vue/dist/vue.js"></script>
 <script src="../node_modules/axios/dist/axios.js"></script>
 <link rel="stylesheet"  integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div id="app" class="container">
 <h1>axios插件講解</h1>
 <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="btn btn-primary" v-on:click="get">Get請(qǐng)求</a>
 <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="btn btn-primary" v-on:click="post">Post請(qǐng)求</a>
 <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="btn btn-primary" v-on:click="http">http</a>
 <div>
 <span>{{this.msg}}</span>
 </div>
</div>
<script>
 new Vue({
 el: '#app',
 data: {
  msg: ''
 },
 mounted () {
  // 請(qǐng)求攔截
  axios.interceptors.request.use(config => {
  return config
  },error => {
  return Promise.reject(error)
  })
  axios.interceptors.response.use(response => {
  // 預(yù)處理相應(yīng)的數(shù)據(jù)
  return response
  }, error => {
  // 錯(cuò)誤返回 狀態(tài)碼驗(yàn)證
  return Promise.reject(error)
  })
 },
 methods: {
  get () {
  axios.get('../package1.json', {
   params: {
   userId: '999'
   },
   headers: {
   token: 'jack'
   }
  }).then(res => {
   this.msg = res.data
  }).catch(error => {
   console.log('error init.' + error)
  })
  },
  post () {
  axios.post('../package.json', {
   userId: '888'
  },{
   headers: {
   token: 'tom'
   }
  }).then(res => {
   this.msg = res.data
  }).catch(error => {
   console.log('error init.' + error)
  })
  },
  http () {
  // 配置請(qǐng)求
  axios({
   url: '../package.json',
   method: 'get',
   // if method is post
   data: {
   userId: '101'
   },
   // if method is get
   params: {
   userId: '102'
   },
   headers: {
   token: 'http-test'
   }
  }).then(res => {
   this.msg = res.data
  }).catch(error => {
   console.log('error init.' + error)
  })
  }
 }
 })
</script>
</body>
</html>

ps:下面看下vue axios數(shù)據(jù)請(qǐng)求get、post方法的使用

我們常用的有g(shù)et方法以及post方法,下面簡(jiǎn)單的介紹一下這兩種請(qǐng)求方法

vue中使用axios方法我們先安裝axios這個(gè)方法

npm install --save axios

安裝之后采用按需引入的方法,哪個(gè)頁(yè)面需要請(qǐng)求數(shù)據(jù)就在哪個(gè)頁(yè)面里引入一下。

import axios from 'axios'

引入之后我們就可以進(jìn)行數(shù)據(jù)請(qǐng)求了,在methods中創(chuàng)建一個(gè)方法

methods:{
 getInfo(){
  let url = "url"
  axios.get(url).then((res)=>{
   console.log(res)
  })  
 } 
}

然后我們?cè)趍ounted這個(gè)生命周期中進(jìn)行調(diào)用

 mounted(){
  this.getInfo() 
 }

這樣就可以在控制臺(tái)中查看數(shù)據(jù),以上是一個(gè)簡(jiǎn)單的get方法數(shù)據(jù)請(qǐng)求,下面繼續(xù)介紹一下post方法的使用,其實(shí)post和get的使用沒(méi)有什么區(qū)別只是再加上一個(gè)參數(shù)就可以了,看一下我們的代碼

methods:{
 postInfo(){
  let url = "url"
  let params=new URLSearchParams();//這個(gè)方法在axios的官網(wǎng)中有介紹,除了這個(gè)方法還有qs這個(gè)方法
  params.append("key",index) 
  params.append("key",index)
  axios.post(url,params).then((res)=>{
   console.log(res)
  })
 } 
}

同樣在mounted這個(gè)生命周期中進(jìn)行調(diào)用

 mounted(){
  this.postInfo()
 }

總結(jié)

以上所述是小編給大家介紹的Vue axios全局?jǐn)r截 get請(qǐng)求、post請(qǐng)求、配置請(qǐng)求的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 詳解在Vue中如何使用provide與inject

    詳解在Vue中如何使用provide與inject

    在vue2.0里面provide與inject是以選項(xiàng)式(配置)API的方式在組件中進(jìn)行使用的,解決的是跨組件(祖孫)間通信的一種方式,本文就來(lái)聊聊它們?cè)赩ue中具體的使用吧
    2023-03-03
  • 詳解Vue+Element的動(dòng)態(tài)表單,動(dòng)態(tài)表格(后端發(fā)送配置,前端動(dòng)態(tài)生成)

    詳解Vue+Element的動(dòng)態(tài)表單,動(dòng)態(tài)表格(后端發(fā)送配置,前端動(dòng)態(tài)生成)

    這篇文章主要介紹了Vue+Element動(dòng)態(tài)表單動(dòng)態(tài)表格,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • vue.js實(shí)現(xiàn)二級(jí)菜單效果

    vue.js實(shí)現(xiàn)二級(jí)菜單效果

    這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)二級(jí)菜單效果的具體方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • Vue3請(qǐng)求攔截器里如何配置token

    Vue3請(qǐng)求攔截器里如何配置token

    這篇文章主要介紹了Vue3請(qǐng)求攔截器里如何配置token,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • vue?數(shù)組添加數(shù)據(jù)方式

    vue?數(shù)組添加數(shù)據(jù)方式

    這篇文章主要介紹了vue?數(shù)組添加數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 淺談vue首屏加載優(yōu)化

    淺談vue首屏加載優(yōu)化

    這篇文章主要介紹了淺談vue首屏加載優(yōu)化,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • electron-builder打包vue2項(xiàng)目問(wèn)題總結(jié)

    electron-builder打包vue2項(xiàng)目問(wèn)題總結(jié)

    這篇文章主要介紹了electron-builder打包vue2項(xiàng)目問(wèn)題總結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-08-08
  • Vue3編寫(xiě)氣泡對(duì)話(huà)框組件

    Vue3編寫(xiě)氣泡對(duì)話(huà)框組件

    這篇文章主要為大家詳細(xì)介紹了Vue3編寫(xiě)氣泡對(duì)話(huà)框組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Vue的生命周期操作示例

    Vue的生命周期操作示例

    這篇文章主要介紹了Vue的生命周期操作,結(jié)合實(shí)例形式分析了vue生命周期中各個(gè)函數(shù)的運(yùn)行步驟,需要的朋友可以參考下
    2019-09-09
  • 使用Axios攔截器中止Vue請(qǐng)求的步驟詳解

    使用Axios攔截器中止Vue請(qǐng)求的步驟詳解

    假設(shè)?App?的用戶(hù)可以在短時(shí)間內(nèi)進(jìn)行多個(gè)?API?調(diào)用,但您只想顯示上次調(diào)用的結(jié)果,之前正在進(jìn)行的請(qǐng)求變得無(wú)關(guān)緊要,在這種情況下,您可以利用?Axios?攔截器,本文給大家介紹了如何使用Axios攔截器中止Vue請(qǐng)求,需要的朋友可以參考下
    2023-11-11

最新評(píng)論