vue 封装 Axios菜鸟教程

1、Axios依赖下载

复制代码
$ npm install axios

2、以下链接为Axios 的api

Axios 实例 | Axios中文文档 | Axios中文网

3、 项目新建request.js,文件名称按照驼峰命名法就可以

4、封装request.js代码如下

复制代码
import axios from "axios"
 
//创建axios实例,设置配置得默认值
 const instance = axios.create({
        baseURL: 'http://localhost:8081',//请求服务端的ip和端口
        timeout: 5000 })
      instance.interceptors.request.use(config => {
        return config
      }, error => {
        return Promise.reject(error)
      })

// 向外暴露axios实例
export default instance

测试代码如下,'@../../../config/request'引用的封装好的request.js文件

复制代码
<template>
  <el-form ref="loginForm" :model="loginForm" label-width="80px">
    <el-form-item label="用户名">
      <el-input v-model="loginForm.username" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="密码">
      <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">登录</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import newrequest from '@../../../config/request'
export default {
  data () {
    return {
      loginForm: {
        username: '111111',
        password: '111111'
      }
    }
  },
  methods: {
    submitForm () {
      // 这里应该是登录逻辑,比如发送请求到后端验证用户名和密码
      // axios({
      //   method: 'post',
      //   url: 'http://localhost:8081/getstudent',
      //   params: { }}).then((res) => { console.log(res.data) })

      console.log('登录表单提交:', this.loginForm)//  console.log('res:', res)
      // const instance = axios.create({
      //   baseURL: 'http://localhost:8081',
      //   timeout: 5000 })
      // instance.interceptors.request.use(config => {
      //   // const token = localStorage.getItem('token')
      //   // if (token) {
      //   //   config.headers.Authorization = `Bearer ${token}`
      //   // }
      //   return config
      // }, error => {
      //   return Promise.reject(error)
      // })
      newrequest.request({
        method: 'post',
        url: '/getstudent'
      }).then(data => {
        console.log('getstudent:' + data)
      }).catch(error => {
        console.error(error)
      })
    }
    // getHomeInfo (params) {
    //   return request({url: '/login', method: 'post', params})
    // }

  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
.el-form {
  width: 240px;
  margin: 200px auto;
}
</style>

原生的Axios写法

复制代码
<template>
  <el-form ref="loginForm" :model="loginForm" label-width="80px">
    <el-form-item label="用户名">
      <el-input v-model="loginForm.username" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="密码">
      <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">登录</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import axios from 'axios'
export default {
  data () {
    return {
      loginForm: {
        username: '111111',
        password: '111111'
      }
    }
  },
  methods: {
    submitForm () {
      axios({
        method: 'post',
        url: 'http://localhost:8081/getstudent',
        params: {}
      }).then((res) => { console.log(res.data) })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
.el-form {
  width: 240px;
  margin: 200px auto;
}
</style>
相关推荐
kong790692836 分钟前
环境搭建-运行前端工程(vue)
前端·前端环境
谷歌开发者41 分钟前
Web 开发指向标|开发者工具 AI 辅助功能的 5 大实践应用
前端·人工智能
kkai人工智能2 小时前
AI写作:从“废话”到“爆款”
开发语言·人工智能·ai·ai写作
lizz315 小时前
C++模板编程:从入门到精通
java·开发语言·c++
shoubepatien6 小时前
JAVA -- 05
java·开发语言
寰天柚子6 小时前
Java并发编程中的线程安全问题与解决方案全解析
java·开发语言·python
沐知全栈开发6 小时前
Bootstrap 下拉菜单:设计与实现指南
开发语言
晚烛6 小时前
实战前瞻:构建高可靠、强协同的 Flutter + OpenHarmony 智慧教育平台
javascript·flutter·html
Evand J6 小时前
【MATLAB例程】多锚点RSSI定位和基站选择方法,基于GDOP、基站距离等因素。以Wi-Fi定位为例,附下载链接
开发语言·matlab·定位·gdop·rssi
superman超哥6 小时前
仓颉语言中锁的实现机制深度剖析与并发实践
c语言·开发语言·c++·python·仓颉