From beb42d4585004be9a4ef39e3dbec6c72377d0f92 Mon Sep 17 00:00:00 2001 From: "453530270@qq.com" Date: Sun, 2 Jul 2023 10:18:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86vue=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E4=B8=AD=E7=9A=84=E8=B7=A8=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 3 +++ src/utils/request.js | 9 ++++++--- src/views/HomeView.vue | 3 +++ vue.config.js | 26 ++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..b5c472a --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +NODE_ENV=production +VUE_APP_PREVIEW=false +VUE_APP_API_BASE_URL=/api \ No newline at end of file diff --git a/src/utils/request.js b/src/utils/request.js index 5eb164e..fcd05c2 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -8,8 +8,9 @@ const publicConfig = window.publicConfig // 创建一个 axios 实例 const service = axios.create({ //baseURL: '/api', // 所有的请求地址前缀部分 - baseURL: publicConfig.BASE_API, + // baseURL: publicConfig.BASE_API, //baseURL:ahost, + baseURL: process.env.NODE_ENV === 'production' ? publicConfig.BASE_API : process.env.VUE_APP_API_BASE_URL, timeout: 60000, // 请求超时时间毫秒 withCredentials: true, // 异步请求携带cookie headers: { @@ -33,7 +34,7 @@ service.interceptors.request.use( // 添加响应拦截器 service.interceptors.response.use( function (response) { - console.log(response) + // console.log(response) // 2xx 范围内的状态码都会触发该函数。 // 对响应数据做点什么 // dataAxios 是 axios 返回数据中的 data @@ -48,8 +49,10 @@ service.interceptors.response.use( function (error) { // 超出 2xx 范围的状态码都会触发该函数。 // 对响应错误做点什么 - console.log(error) + // console.log(error) return Promise.reject(error) } ) + + export default service diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index 908c5d2..6cf74a2 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -175,6 +175,9 @@ export default { // btApi.allGroupTimeNum({'granularity':'daily'}).then(res=>{ console.log(res) + }).catch(err=>{ + console.log("发生错误:") + console.log(err) }); }, //选择年月日中的某个 diff --git a/vue.config.js b/vue.config.js index a91654c..74d8d1d 100644 --- a/vue.config.js +++ b/vue.config.js @@ -1,5 +1,31 @@ const { defineConfig } = require('@vue/cli-service') + module.exports = defineConfig({ + // 部署应用时候的基本url + //publicPath: process.env.NODE_ENV === 'production' ? './' : '/', + + devServer: { + proxy: { + '/api': { + target: 'http://btgym.xingtongworld.com/api/', // 配置访问的服务器地址 + pathRewrite: { '^/api': '' }, // 用于将请求中的 /api 字符串替换为空, 然后访问地址就能正确访问,若不添加此行配置,那么访问地址就变成了: http://localhost:5000/api/request_url,这样的请求就会出现 404 操作 + ws: true, // 是否支持 webstocket, 默认是 true + changeOrigin: true // 用于控制请求头中的 host 值, 默认是 ture + } + }, + // http 代理配置 + // proxy: { + // '/m': { + // target: 'http://btgym.xingtongworld.com/', //目标接口域名 + // changeOrigin: true, //是否跨域 + // // logLevel: "debug", + // pathRewrite: { + // '^/m': '/api' + // } + // } + // }, + }, + transpileDependencies: true, lintOnSave: false, publicPath: './'