From 8c9542ec23b02050c2c85f91191fa32cdbf5ad8c Mon Sep 17 00:00:00 2001 From: work Date: Wed, 4 Dec 2024 11:16:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=B9=E5=87=BA=E5=AF=B9=E8=AF=9D=E6=A1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- C3-axios/demo2/src/api/request.js | 23 ++++++-- C3-axios/demo2/src/views/users/Dialog.vue | 59 +++++++++++++++++++++ C3-axios/demo2/src/views/users/Students.vue | 21 +++++++- 3 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 C3-axios/demo2/src/views/users/Dialog.vue diff --git a/C3-axios/demo2/src/api/request.js b/C3-axios/demo2/src/api/request.js index 9490048..ff455d5 100644 --- a/C3-axios/demo2/src/api/request.js +++ b/C3-axios/demo2/src/api/request.js @@ -1,5 +1,5 @@ import axios from "axios"; -import { ElMessage } from "element-plus"; +import { ElMessage } from 'element-plus'; const axiosInstance = axios.create({ baseURL: 'http://localhost:8080/api', timeout: 5000, @@ -22,9 +22,26 @@ axiosInstance.interceptors.response.use(response => { } }, error => { // 处理响应错误 - // 如果error对象包含response属性,并且response对象包含data属性,显示错误消息 + // 检查错误对象 error 是否包含 response 属性,并且如果包含,则使用 ElMessage.error 显示错误信息 error.response && ElMessage.error(error.response.data) // 返回一个拒绝的Promise,其原因是error对象的response的data属性 - // return Promise.reject(error.response.data) + return Promise.reject(error.response) }) + + +axiosInstance.interceptors.request.use( config => { + /*const token = localStorage.getItem('token') + console.log(token) + if(token){ + config.headers.Authorization = `Bearer ${token}` + }*/ + return config +}, error => { + // 处理响应错误 + // 检查错误对象 error 是否包含 response 属性,并且如果包含,则使用 ElMessage.error 显示错误信息 + error.response && ElMessage.error(error.response.data) + // 返回一个拒绝的Promise,其原因是error对象的response的data属性 + return Promise.reject(error.response) +}) + export default axiosInstance; \ No newline at end of file diff --git a/C3-axios/demo2/src/views/users/Dialog.vue b/C3-axios/demo2/src/views/users/Dialog.vue new file mode 100644 index 0000000..629c070 --- /dev/null +++ b/C3-axios/demo2/src/views/users/Dialog.vue @@ -0,0 +1,59 @@ + + + + + \ No newline at end of file diff --git a/C3-axios/demo2/src/views/users/Students.vue b/C3-axios/demo2/src/views/users/Students.vue index 8e26f4a..973a33a 100644 --- a/C3-axios/demo2/src/views/users/Students.vue +++ b/C3-axios/demo2/src/views/users/Students.vue @@ -25,7 +25,7 @@ const handleSearch = async () => { } } initGetStudents() - +const centerDialogVisible = ref(false)