axios基础封装

request.js

新建文件夹 network 在文件新建 request.js

import axios from 'axios'
export function request (config) {
  // 1创建axios实例
  const intstance = axios.create({
    baseURL: 'https://localhost:44314',
    timeout: 5000
    // contentType: 'application/json;charset=UTF-8',
    // dataType: 'json'
  })
  // 1.axios拦截器
  intstance.interceptors.request.use(config => {
    return config
  }, er => {})
  // 1.axios响应拦截拦截
  intstance.interceptors.response.use(res => {
    return res
  }, er => {
  })
  return intstance(config)
}
export default request //导出

main.挂载配置

import request from '@/network/request'

new Vue({
  el: '#app',
  router,
  store,
  request, // 封装的axios
  components: { App },
  template: '<App/>'

})

vue组件调用

在需要使用axios的vue组件调用

import request from '@/network/request'

  request({ // 查询所有
        url: '/api/Gj'
      }).then(res => {
        this.tableData = res.data
      })

 request({ // 条件查询
        url: '/api/Gjtype/' + gjtype
      }).then(res => {
        this.tableData = res.data
      })


 request({ // 添加
        url: '/api/Gj',
        method: 'POST',
        data: {
          'id': 0,
          'method': this.method,
          'text': this.content,
          'type': this.value
        }
      }).then(res => {
        console.log(res)
        if (res.status === 201) {
          this.open1()
          console.log('成功')
        } else {
          this.open4()
          console.log('失败')
        }
      }).catch(console.error.bind(console)) // 异常


request({// 更新
        url: '/api/Gj/' + this.id,
        method: 'put',
        data: {
          'id': this.newinfo.id,
          'method': this.newinfo.method,
          'text': this.newinfo.text,
          'type': this.newinfo.type
       }
      }).then(res => {
        if (res.status === 204) {
          this.open1()
          console.log('成功')
        } else {
          this.open4()
          console.log('失败')
        }
      }).catch(console.error.bind(console)) // 异常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值