vue3封装接口

简介: vue3封装接口

在src下面创建一个文件夹任意名称

我拿这个名字举例子了apiService

相当于创建一个新的文件

// 封装接口
// apiService.js
import axios from 'axios';
// 接口前缀
const API_BASE_URL = '前缀';
接口后缀
export const registerUser = async (fileData) => {
  try {
    const response = await axios.post(`${API_BASE_URL}/index/index`, fileData);
    return response.data;
  } catch (error) {
    console.error('Error uploading file:', error);
    throw error;
  }
};
export const registerUsers = async (fileData) => {
  try {
    const response = await axios.post(`${API_BASE_URL}/index/indexs`, fileData);
    return response.data;
  } catch (error) {
    console.error('Error uploading file:', error);
    throw error;
  }
};

用到的页面

<template>
 <div>
  <div @click="handleLogin">点击获取数据</div>
  <div>1</div>
  <div>1</div>
  <div>1</div>
  <div>1</div>
 </div>
</template>
<script setup>
import { ref } from 'vue';
import { registerUser } from "../apiService/apiService.js";
const credentials = ref({/* 数据 */});
// 示例:用户登录
const handleLogin = async () => {
  try {
    const response = await registerUser(credentials.value);
    console.log('index信息:', response);
  } catch (error) {
    console.error('Login failed:', error);
  }
};
</script>

大致就是这样了

目录
相关文章
|
1天前
|
JavaScript 定位技术 API
在 vue3 中使用高德地图
在 vue3 中使用高德地图
8 0
|
1天前
vue3 键盘事件 回车发送消息,ctrl+回车 内容换行
const textarea = textInput.value.textarea; //获取输入框元素
9 3
|
4天前
|
JavaScript 前端开发 CDN
vue3速览
vue3速览
14 0
|
4天前
|
设计模式 JavaScript 前端开发
Vue3报错Property “xxx“ was accessed during render but is not defined on instance
Vue3报错Property “xxx“ was accessed during render but is not defined on instance
|
4天前
|
JavaScript 前端开发 安全
Vue3官方文档速通(下)
Vue3官方文档速通(下)
15 0
|
4天前
|
JavaScript API
Vue3 官方文档速通(中)
Vue3 官方文档速通(中)
20 0
|
4天前
|
缓存 JavaScript 前端开发
Vue3 官方文档速通(上)
Vue3 官方文档速通(上)
26 0
|
4天前
Vue3+Vite+Pinia+Naive后台管理系统搭建之五:Pinia 状态管理
Vue3+Vite+Pinia+Naive后台管理系统搭建之五:Pinia 状态管理
8 1
|
4天前
Vue3+Vite+Pinia+Naive后台管理系统搭建之三:vue-router 的安装和使用
Vue3+Vite+Pinia+Naive后台管理系统搭建之三:vue-router 的安装和使用
10 0
|
4天前
Vue3+Vite+Pinia+Naive后台管理系统搭建之二:scss 的安装和使用
Vue3+Vite+Pinia+Naive后台管理系统搭建之二:scss 的安装和使用
8 0