当前位置:首页 > VUE

vue实现axio请求

2026-03-29 21:39:58VUE

安装 Axios

在 Vue 项目中安装 Axios,可以通过 npm 或 yarn 完成:

npm install axios
yarn add axios

引入 Axios

在需要发送请求的组件或全局配置中引入 Axios:

import axios from 'axios';

全局配置 Axios

可以在 main.js 或入口文件中全局配置 Axios,例如设置默认的请求头或基地址:

axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers.common['Authorization'] = 'Bearer token';

发送 GET 请求

使用 Axios 发送 GET 请求获取数据:

axios.get('/users')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

发送 POST 请求

发送 POST 请求提交数据:

vue实现axio请求

axios.post('/users', { name: 'John', age: 30 })
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

发送 PUT 请求

更新数据可以使用 PUT 请求:

axios.put('/users/1', { name: 'John', age: 31 })
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

发送 DELETE 请求

删除数据可以使用 DELETE 请求:

axios.delete('/users/1')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

拦截请求和响应

Axios 支持拦截器,可以在请求或响应被处理前进行拦截:

vue实现axio请求

axios.interceptors.request.use(config => {
  console.log('Request sent:', config);
  return config;
}, error => {
  return Promise.reject(error);
});

axios.interceptors.response.use(response => {
  console.log('Response received:', response);
  return response;
}, error => {
  return Promise.reject(error);
});

使用 async/await

结合 async/await 可以更简洁地处理异步请求:

async function fetchUsers() {
  try {
    const response = await axios.get('/users');
    console.log(response.data);
  } catch (error) {
    console.error(error);
  }
}

错误处理

统一处理请求错误:

axios.get('/users')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    if (error.response) {
      console.error('Server responded with error:', error.response.status);
    } else if (error.request) {
      console.error('No response received:', error.request);
    } else {
      console.error('Request setup error:', error.message);
    }
  });

取消请求

使用 CancelToken 可以取消未完成的请求:

const CancelToken = axios.CancelToken;
const source = CancelToken.source();

axios.get('/users', {
  cancelToken: source.token
})
  .then(response => {
    console.log(response.data);
  })
  .catch(thrown => {
    if (axios.isCancel(thrown)) {
      console.log('Request canceled:', thrown.message);
    } else {
      console.error(thrown);
    }
  });

// 取消请求
source.cancel('Operation canceled by the user.');

标签: vueaxio
分享给朋友:

相关文章

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…

vue实现队列

vue实现队列

Vue 实现队列功能 在 Vue 中实现队列功能可以通过多种方式完成,以下是几种常见的方法: 使用数组模拟队列 队列遵循先进先出(FIFO)原则,可以用数组的 push 和 shift 方法模拟入队…

vue实现指引

vue实现指引

Vue 实现指引功能 Vue 中实现指引功能可以通过多种方式完成,常见的有使用第三方库或自定义实现。以下是几种常见方法: 使用第三方库(如 driver.js) 安装 driver.js: np…

vue ui实现创建vue项目

vue ui实现创建vue项目

使用 Vue UI 创建 Vue 项目 Vue UI 是 Vue CLI 提供的图形化界面工具,可以通过可视化操作创建和管理 Vue 项目。 安装 Vue CLI 确保已安装 Node.js(建议版…

vue实现滚动监听

vue实现滚动监听

滚动监听的基本实现 在Vue中实现滚动监听可以通过原生JavaScript的window.addEventListener或结合Vue的生命周期钩子完成。以下是一个基础示例: export defa…

vue滚动加载实现

vue滚动加载实现

vue滚动加载实现 滚动加载是一种常见的优化手段,通过监听滚动事件动态加载数据,减少初始渲染压力。以下是几种实现方式: 使用IntersectionObserver API Intersection…