vue 封装 Axios菜鸟教程

[复制链接]
发表于 2025-10-8 07:04:00 | 显示全部楼层 |阅读模式
1、Axios依靠下载
  
  1. $ npm install axios
复制代码
 2、以下链接为Axios 的api
Axios 实例 | Axios中文文档 | Axios中文网
3、  项目新建request.js,文件名称按照驼峰定名法就可以

4、封装request.js代码如下
  1. import axios from "axios"
  2. //创建axios实例,设置配置得默认值
  3. const instance = axios.create({
  4.         baseURL: 'http://localhost:8081',//请求服务端的ip和端口
  5.         timeout: 5000 })
  6.       instance.interceptors.request.use(config => {
  7.         return config
  8.       }, error => {
  9.         return Promise.reject(error)
  10.       })
  11. // 向外暴露axios实例
  12. export default instance
复制代码
 测试代码如下,'@../../../config/request'引用的封装好的request.js文件
  1. <template>
  2.   <el-form ref="loginForm" :model="loginForm" label-width="80px">
  3.     <el-form-item label="用户名">
  4.       <el-input v-model="loginForm.username" autocomplete="off"></el-input>
  5.     </el-form-item>
  6.     <el-form-item label="密码">
  7.       <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
  8.     </el-form-item>
  9.     <el-form-item>
  10.       <el-button type="primary" @click="submitForm">登录</el-button>
  11.     </el-form-item>
  12.   </el-form>
  13. </template>
  14. <script>
  15. import newrequest from '@../../../config/request'
  16. export default {
  17.   data () {
  18.     return {
  19.       loginForm: {
  20.         username: '111111',
  21.         password: '111111'
  22.       }
  23.     }
  24.   },
  25.   methods: {
  26.     submitForm () {
  27.       // 这里应该是登录逻辑,比如发送请求到后端验证用户名和密码
  28.       // axios({
  29.       //   method: 'post',
  30.       //   url: 'http://localhost:8081/getstudent',
  31.       //   params: { }}).then((res) => { console.log(res.data) })
  32.       console.log('登录表单提交:', this.loginForm)//  console.log('res:', res)
  33.       // const instance = axios.create({
  34.       //   baseURL: 'http://localhost:8081',
  35.       //   timeout: 5000 })
  36.       // instance.interceptors.request.use(config => {
  37.       //   // const token = localStorage.getItem('token')
  38.       //   // if (token) {
  39.       //   //   config.headers.Authorization = `Bearer ${token}`
  40.       //   // }
  41.       //   return config
  42.       // }, error => {
  43.       //   return Promise.reject(error)
  44.       // })
  45.       newrequest.request({
  46.         method: 'post',
  47.         url: '/getstudent'
  48.       }).then(data => {
  49.         console.log('getstudent:' + data)
  50.       }).catch(error => {
  51.         console.error(error)
  52.       })
  53.     }
  54.     // getHomeInfo (params) {
  55.     //   return request({url: '/login', method: 'post', params})
  56.     // }
  57.   }
  58. }
  59. </script>
  60. <!-- Add "scoped" attribute to limit CSS to this component only -->
  61. <style scoped>
  62. h1, h2 {
  63.   font-weight: normal;
  64. }
  65. ul {
  66.   list-style-type: none;
  67.   padding: 0;
  68. }
  69. li {
  70.   display: inline-block;
  71.   margin: 0 10px;
  72. }
  73. a {
  74.   color: #42b983;
  75. }
  76. .el-form {
  77.   width: 240px;
  78.   margin: 200px auto;
  79. }
  80. </style>
复制代码
原生的Axios写法
  1. <template>
  2.   <el-form ref="loginForm" :model="loginForm" label-width="80px">
  3.     <el-form-item label="用户名">
  4.       <el-input v-model="loginForm.username" autocomplete="off"></el-input>
  5.     </el-form-item>
  6.     <el-form-item label="密码">
  7.       <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
  8.     </el-form-item>
  9.     <el-form-item>
  10.       <el-button type="primary" @click="submitForm">登录</el-button>
  11.     </el-form-item>
  12.   </el-form>
  13. </template>
  14. <script>
  15. import axios from 'axios'
  16. export default {
  17.   data () {
  18.     return {
  19.       loginForm: {
  20.         username: '111111',
  21.         password: '111111'
  22.       }
  23.     }
  24.   },
  25.   methods: {
  26.     submitForm () {
  27.       axios({
  28.         method: 'post',
  29.         url: 'http://localhost:8081/getstudent',
  30.         params: {}
  31.       }).then((res) => { console.log(res.data) })
  32.     }
  33.   }
  34. }
  35. </script>
  36. <!-- Add "scoped" attribute to limit CSS to this component only -->
  37. <style scoped>
  38. h1, h2 {
  39.   font-weight: normal;
  40. }
  41. ul {
  42.   list-style-type: none;
  43.   padding: 0;
  44. }
  45. li {
  46.   display: inline-block;
  47.   margin: 0 10px;
  48. }
  49. a {
  50.   color: #42b983;
  51. }
  52. .el-form {
  53.   width: 240px;
  54.   margin: 200px auto;
  55. }
  56. </style>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表