当前位置:首页 > VUE

vue实现get请求

2026-03-09 07:31:01VUE

使用 Vue 实现 GET 请求

在 Vue 中实现 GET 请求可以通过多种方式,常见的有使用原生 fetchaxios 或 Vue 的 $http(若配置了相关插件)。以下是几种典型方法:

使用 Axios(推荐)

Axios 是一个流行的 HTTP 客户端,支持 Promise API,适合在 Vue 中使用。

  1. 安装 Axios
    通过 npm 或 yarn 安装:

    npm install axios
  2. 在组件中发起 GET 请求
    在 Vue 组件的 methods 或生命周期钩子(如 created)中调用:

    import axios from 'axios';
    
    export default {
      data() {
        return {
          responseData: null,
          error: null
        };
      },
      created() {
        axios.get('https://api.example.com/data')
          .then(response => {
            this.responseData = response.data;
          })
          .catch(error => {
            this.error = error.message;
          });
      }
    };

使用 Fetch API

原生 fetch 是浏览器提供的 API,无需额外安装。

export default {
  data() {
    return {
      responseData: null,
      error: null
    };
  },
  created() {
    fetch('https://api.example.com/data')
      .then(response => {
        if (!response.ok) throw new Error('Network error');
        return response.json();
      })
      .then(data => {
        this.responseData = data;
      })
      .catch(error => {
        this.error = error.message;
      });
  }
};

使用 Vue Resource(已废弃)

Vue Resource 曾是官方推荐的库,但目前已不再维护。仅适用于遗留项目。

  1. 安装 Vue Resource

    npm install vue-resource
  2. 在入口文件中启用

    import Vue from 'vue';
    import VueResource from 'vue-resource';
    
    Vue.use(VueResource);
  3. 发起请求

    export default {
      created() {
        this.$http.get('https://api.example.com/data')
          .then(response => {
            this.responseData = response.body;
          })
          .catch(error => {
            this.error = error.message;
          });
      }
    };

参数传递

若需传递查询参数,Axios 和 Fetch 的方式略有不同:

  • Axios

    axios.get('https://api.example.com/data', {
      params: { id: 1, limit: 10 }
    });
  • Fetch

    const params = new URLSearchParams({ id: 1, limit: 10 });
    fetch(`https://api.example.com/data?${params}`);

全局配置(Axios)

可设置默认 baseURL 或拦截器,避免重复代码:

vue实现get请求

// main.js 或单独配置文件
axios.defaults.baseURL = 'https://api.example.com';
axios.interceptors.response.use(
  response => response.data,
  error => Promise.reject(error)
);

注意事项

  1. 错误处理
    务必捕获请求异常,避免未处理的 Promise 错误。
  2. 跨域问题
    若接口跨域,需后端配置 CORS 或前端通过代理解决。
  3. 取消请求
    使用 Axios 的 CancelToken 或 Fetch 的 AbortController 取消未完成的请求。

标签: vueget
分享给朋友:

相关文章

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

vue实现app

vue实现app

Vue 实现 App 的常见方法 使用 Vue 开发移动端 App 主要有两种主流方案:混合开发(Hybrid)和原生渲染。以下是具体实现方式和工具链: 混合开发方案(WebView 嵌入) 通过…

vue实现购票

vue实现购票

Vue 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…

vue实现Siri

vue实现Siri

Vue 实现 Siri 风格语音交互 实现类似 Siri 的语音交互功能需要结合语音识别、语音合成和前端交互逻辑。Vue 作为前端框架,可以很好地管理这些功能的交互状态。 语音识别集成 使用 Web…

vue实现 treegrid

vue实现 treegrid

Vue 实现 TreeGrid 的方法 使用现成组件库 Element UI、Ant Design Vue 等流行组件库提供了现成的 TreeTable/TreeGrid 组件,可直接使用。 Ele…

vue实现数据

vue实现数据

Vue 实现数据绑定的方法 Vue.js 提供了多种方式实现数据绑定,包括双向绑定、单向绑定以及动态数据绑定。以下是几种常见的实现方式: 双向数据绑定(v-model) 双向数据绑定通常用于表单元素…