当前位置: 首页 > article >正文

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>


http://www.kler.cn/a/610281.html

相关文章:

  • C++学习之路:从头搞懂配置VScode开发环境的逻辑与步骤
  • P1464 Function —— 洛谷
  • C++:重载操作符
  • django入门教程之cookie和session【六】
  • Pyecharts入门之绘制地图数据
  • 云端存储新纪元:SAN架构驱动的智能网盘解决方案
  • 高维小样本数据的在线流特征选择
  • LangChain开发(二)LangChain提示词模板Template使用
  • LeetCode Hot 100 - 矩阵 | 73.矩阵置零、54.螺旋矩阵、48.旋转图像、240.搜索二维矩阵II
  • STM32F103_LL库+寄存器学习笔记03 - GPIO设置输入模式,并轮询GPIO的电平状态
  • Day15 -实例 端口扫描工具 WAF识别工具的使用
  • 推荐一个可以自定义github主页的网站
  • WinSCP使用教程:(SFTP、SCP、FTP 和 WebDAV)
  • 【深度学习】扩散模型(Diffusion Model)详解:原理、应用与当前进展
  • 阿波罗Apollo相关配置
  • 计算机视觉3——模板匹配与拟合
  • 如何在 HTML 中嵌入外部字体,有哪些注意事项?
  • Java EE(12)——初始网络
  • 《索引江湖:B树索引与哈希索引的风云对决》
  • Rust从入门到精通之进阶篇:14.并发编程