zoukankan      html  css  js  c++  java
  • Vue 封装网络工具类

    1.创建nextwork.js

    import axios from 'axios'
    
    // 进行一些全局配置
    
    axios.defaults.baseURL = 'http://127.0.0.1:3000'
    axios.defaults.timeout = 3000
    
    // 封装组件的get/post方法
    
    export default {
      get: function (path = '', data = {}) {
        return new Promise(function (resolve, reject) {
          axios.get(path, {
            params: data
          })
            .then(function (response) {
              resolve(response)
            })
            .catch(function (error) {
              reject(error)
            })
        })
      },
      post: function (path = '', data = {}) {
        return new Promise(function (resolve, reject) {
          axios.post(path, data)
            .then(function (response) {
              resolve(response)
            })
            .catch(function (error) {
              reject(error)
            })
        })
      }
    }
    

      2.创建index.js

    import NextWork from './nextwork'
    
    export const getBanner = () => NextWork.get('banner')
    

      3.测试网络工具类

    <script>
    import { getBanner } from '@/api'
    
    export default {
      name: 'Recommend',
      created () {
        getBanner().then(function (data) {
          console.log(data)
        })
          .catch(function (err) {
            console.log(err)
          })
      }
    }
    </script>
    

      

  • 相关阅读:
    [IOI2013]Dreaming
    Lost Cows
    Mobile Service
    [POI2005]Bank notes
    [CTSC2007]动物园zoo
    [CF1093F]Vasya and Array
    [雅礼集训 2017 Day1]市场
    [APIO2014]序列分割
    [CEOI2004]锯木厂选址
    [APIO2010]特别行动队
  • 原文地址:https://www.cnblogs.com/WorldEye/p/13786523.html
Copyright © 2011-2022 走看看