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

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

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

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

一、utils下的httpUtils.js:

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

const errorHandler = (status, info) => {
    switch(status){
        case 400:
            console.log("400 - 語(yǔ)義錯(cuò)誤");
            break;
        case 401:
            console.log("401 - 服務(wù)器認(rèn)證失敗");
            break;
        case 403:
            console.log("403 - 服務(wù)器拒絕訪問");
            break;
        case 404:
            console.log("404 - 地址錯(cuò)誤");
            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實(shí)例
const instance = axios.create({
    // 放置網(wǎng)絡(luò)請(qǐng)求的公共參數(shù)
    timeout:5000, // 5s后超時(shí)
})

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

        // config中存在網(wǎng)絡(luò)請(qǐng)求的所有信息
        return config;
    },
    error =>{
        return Promise.reject(error)
    }
)
// 2、接收響應(yīng)后
instance.interceptors.response.use(
    response => {
        // 三目運(yùn)算根據(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;

四、組件中引入并請(qǐng)求:

<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的一部分,它返回一個(gè)Promise實(shí)例對(duì)象,用于獲取后臺(tái)返回的數(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>

查看效果:

請(qǐng)求成功

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

相關(guān)文章

  • vue中component組件的props使用詳解

    vue中component組件的props使用詳解

    本篇文章主要介紹了vue中component組件的props使用詳解,這里整理了詳細(xì)的用法,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-09-09
  • Vue實(shí)現(xiàn)炫酷的代碼瀑布流背景

    Vue實(shí)現(xiàn)炫酷的代碼瀑布流背景

    這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)炫酷的代碼瀑布流背景,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Vue2.X 通過AJAX動(dòng)態(tài)更新數(shù)據(jù)

    Vue2.X 通過AJAX動(dòng)態(tài)更新數(shù)據(jù)

    這篇文章主要介紹了Vue2.X 通過AJAX動(dòng)態(tài)更新數(shù)據(jù)的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-07-07
  • Vue中transition標(biāo)簽的基本使用教程

    Vue中transition標(biāo)簽的基本使用教程

    Vue提供了transition的封裝組件,可以給任何元素和組件添加進(jìn)入/離開過渡,下面這篇文章主要給大家介紹了關(guān)于Vue中transition標(biāo)簽基本使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • 解決elementui表格操作列自適應(yīng)列寬

    解決elementui表格操作列自適應(yīng)列寬

    這篇文章主要介紹了解決elementui表格操作列自適應(yīng)列寬,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • vue實(shí)現(xiàn)計(jì)步器功能

    vue實(shí)現(xiàn)計(jì)步器功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)計(jì)步器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Vue Element前端應(yīng)用開發(fā)之動(dòng)態(tài)菜單和路由的關(guān)聯(lián)處理

    Vue Element前端應(yīng)用開發(fā)之動(dòng)態(tài)菜單和路由的關(guān)聯(lián)處理

    這篇文章主要介紹了Vue Element前端應(yīng)用開發(fā)之動(dòng)態(tài)菜單和路由的關(guān)聯(lián)處理,對(duì)vue感興趣的同學(xué),可以參考下
    2021-05-05
  • Vue服務(wù)端渲染實(shí)踐之Web應(yīng)用首屏耗時(shí)最優(yōu)化方案

    Vue服務(wù)端渲染實(shí)踐之Web應(yīng)用首屏耗時(shí)最優(yōu)化方案

    這篇文章主要介紹了Vue服務(wù)端渲染實(shí)踐之Web應(yīng)用首屏耗時(shí)最優(yōu)化方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • vue日志之如何用select選中默認(rèn)值

    vue日志之如何用select選中默認(rèn)值

    這篇文章主要介紹了vue日志之如何select選中默認(rèn)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 詳解Vue取消eslint語(yǔ)法限制

    詳解Vue取消eslint語(yǔ)法限制

    本篇文章給大家分享了Vue取消eslint語(yǔ)法限制的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以參考學(xué)習(xí)下。
    2018-08-08

最新評(píng)論

沙田区| 永善县| 益阳市| 上林县| 临潭县| 隆回县| 都昌县| 衡水市| 汉川市| 金秀| 吉木乃县| 外汇| 浏阳市| 青州市| 江华| 达拉特旗| 津市市| 台中县| 白玉县| 白山市| 石首市| 登封市| 伊金霍洛旗| 丰镇市| 兴安盟| 宣化县| 沿河| 盐池县| 宁安市| 辽阳市| 天峻县| 古田县| 嫩江县| 西平县| 锦屏县| 黄大仙区| 芦山县| 新巴尔虎右旗| 福清市| 仲巴县| 普兰店市|