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>
    

      

  • 相关阅读:
    [Unity3D]计时器/Timer
    unity缓存和浏览器缓存
    unity3d进行脚本资源打包加载
    Unity3d删除无用的美术资源
    项目经理的职责(转载)
    LINQ
    生意经
    Android ListView标题置顶效果实现
    ListView的自动循环滚动显示
    郭霖的专栏
  • 原文地址:https://www.cnblogs.com/WorldEye/p/13786523.html
Copyright © 2011-2022 走看看