在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>
大致就是這樣了