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

Vue簡單封裝axios網(wǎng)絡(luò)請求的方法

 更新時間:2022年11月15日 14:33:11   作者:隨筆都是學(xué)習(xí)筆記  
這篇文章主要介紹了Vue簡單封裝axios網(wǎng)絡(luò)請求,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,對Vue封裝axios網(wǎng)絡(luò)請求相關(guān)知識感興趣的朋友一起看看吧

Vue簡單封裝axios網(wǎng)絡(luò)請求

一、utils下的httpUtils.js:

import axios from 'axios';
import querystring from 'querystring';

const errorHandler = (status, info) => {
    switch(status){
        case 400:
            console.log("400 - 語義錯誤");
            break;
        case 401:
            console.log("401 - 服務(wù)器認(rèn)證失敗");
            break;
        case 403:
            console.log("403 - 服務(wù)器拒絕訪問");
            break;
        case 404:
            console.log("404 - 地址錯誤");
            break;
        case 500:
            console.log("500 - 服務(wù)器遇到意外");
            break;
        case 502:
            console.log("502 - 服務(wù)器無響應(yīng)");
            break;
        default:
            console.log(info);
            break;
    }
}

// 創(chuàng)建axios實例
const instance = axios.create({
    // 放置網(wǎng)絡(luò)請求的公共參數(shù)
    timeout:5000, // 5s后超時
})

// 攔截器最常用
// 1、發(fā)送請求之前
instance.interceptors.request.use(
    config =>{
        if (config.method === 'post'){
            config.data = querystring.stringify(config.data)
        }

        // config中存在網(wǎng)絡(luò)請求的所有信息
        return config;
    },
    error =>{
        return Promise.reject(error)
    }
)
// 2、接收響應(yīng)后
instance.interceptors.response.use(
    response => {
        // 三目運算根據(jù)狀態(tài)碼來判斷接收數(shù)據(jù)還是拒絕數(shù)據(jù)
        return response.status === 200 ? Promise.resolve(response):Promise.reject(response)
    },
    error=>{
        const { response } = error;
        // 自定義方法來輸出異常信息
        errorHandler(response.status,response.info)
    }
)

// 導(dǎo)出
export default instance;

二、/api下的path.js:

const base = {
    // 公共路徑
    baseUrl : "http://iwenwiki.com",
    chengpin : "/api/blueberrypai/getChengpinDetails.php",
    login : "/api/blueberrypai/login.php"
}

export default base;

三、/api下的index.js:

import axios from "../utils/httpUtils";
import path from "./path"

const api = {
    // 成品地址
    getChengpin(){
        return axios.get(path.baseUrl+path.chengpin)
    }
}

export default api;

四、組件中引入并請求:

<template>
  <div>
    <p>{{ msg }}</p>
  </div>
  
</template>

<script>
// import axios from 'axios';
// import QueryString from 'qs';
import api from "../api/index"


export default {
  name: 'HelloWorld',
  data(){
    return{
      msg:"111",
    }
  },
  mounted(){
        //Fetch API 基本用法
    // fetch('http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php').then(function(data){
    //   // text()方法屬于fetchAPI的一部分,它返回一個Promise實例對象,用于獲取后臺返回的數(shù)據(jù)
    //   return data.json();
    // }).then(function(data){
    //   console.log(data.chengpinDetails[0].title);
    // })
    // get
    // axios({
    //   method:"get",
    //   url:"http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php",
    // }).then(res=>{
    //   this.msg=res.data.chengpinDetails[0].title
    // })
    // post
    // axios({
    //   method:"post",
    //   url:"http://iwenwiki.com/api/blueberrypai/login.php",
    //   data: QueryString.stringify({
    //     user_id: "iwen@qq.com",
    //     password: "iwen123",
    //     verification_code: "crfvw"
    //   })
    // }).then(res=>{
    //   this.msg=res.data
    // })
      // 第二種get方法
    // axios.get('http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php').then(res=>{
    //   this.msg=res.data.chengpinDetails[0].title
    // })
      // 第二種post方法
    // this.$axios.post('http://iwenwiki.com/api/blueberrypai/login.php',QueryString.stringify({
    //   user_id: "iwen@qq.com",
    //   password: "iwen123",
    //   verification_code: "crfvw"
    // })).then(res=>{
    //   this.msg=res.data.success
    // })

    api.getChengpin().then(res=>{
      console.log(res.data)
    })
  }  
}
</script>

<style scoped>

</style>

查看效果:

請求成功

到此這篇關(guān)于Vue簡單封裝axios網(wǎng)絡(luò)請求的文章就介紹到這了,更多相關(guān)Vue封裝axios網(wǎng)絡(luò)請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue.js學(xué)習(xí)筆記之 helloworld

    Vue.js學(xué)習(xí)筆記之 helloworld

    vue是法語中視圖的意思,Vue.js是一個輕巧、高性能、可組件化的MVVM庫,同時擁有非常容易上手的API。有需要的小伙伴可以參考下
    2016-08-08
  • Vue 項目部署到服務(wù)器的問題解決方法

    Vue 項目部署到服務(wù)器的問題解決方法

    本篇文章主要介紹了Vue 項目部署到服務(wù)器的問題解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • vue3使用localStorage實現(xiàn)登錄注冊功能實例

    vue3使用localStorage實現(xiàn)登錄注冊功能實例

    這篇文章主要給大家介紹了關(guān)于vue3使用localStorage實現(xiàn)登錄注冊功能的相關(guān)資料, localStorage這個特性主要是用來作為本地存儲來使用的,解決了cookie存儲空間不足的問題,需要的朋友可以參考下
    2023-06-06
  • Vue表單快速上手

    Vue表單快速上手

    工作中vue表單使用的最多的莫過于input、textarea、select等,原生js的基礎(chǔ)上vue通過雙向數(shù)據(jù)綁定等,實現(xiàn)了自己獨有的一套指令,這是react中沒有的部分,也算是vue的一大特色
    2022-09-09
  • vscode配置@路徑提示方式,并解決vue文件內(nèi)失效的問題

    vscode配置@路徑提示方式,并解決vue文件內(nèi)失效的問題

    這篇文章主要介紹了vscode配置@路徑提示方式,并解決vue文件內(nèi)失效的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vue項目中跨域問題解決方案

    Vue項目中跨域問題解決方案

    本文給大家介紹了vue項目中跨域問題的完美解決方案,通過更改header,使用http-proxy-middleware 代理解決(項目使用vue-cli腳手架搭建),具體內(nèi)容詳情大家跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-06-06
  • Vue Socket.io源碼解讀

    Vue Socket.io源碼解讀

    這篇文章主要介紹了Vue Socket.io源碼解讀,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • Vue使用輪詢定時發(fā)送請求代碼

    Vue使用輪詢定時發(fā)送請求代碼

    這篇文章主要介紹了Vue使用輪詢定時發(fā)送請求代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Vue-Ant Design Vue-普通及自定義校驗實例

    Vue-Ant Design Vue-普通及自定義校驗實例

    這篇文章主要介紹了Vue-Ant Design Vue-普通及自定義校驗實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Vue3通過ref操作Dom元素及hooks的使用方法

    Vue3通過ref操作Dom元素及hooks的使用方法

    這篇文章主要介紹了Vue3通過ref操作Dom元素及hooks的使用方法,需要的朋友可以參考下
    2023-01-01

最新評論