1
0
Fork 0

学习开发与生产环境变量配置,方便本地调试

This commit is contained in:
lemons 2025-04-23 23:39:16 +08:00
parent 191ec3e11f
commit 8e567ef883
7 changed files with 28 additions and 4 deletions

2
.env.development Normal file
View File

@ -0,0 +1,2 @@
VITE_BASE_URL=''
VITE_BASE_PATH=/api

2
.env.production Normal file
View File

@ -0,0 +1,2 @@
VITE_BASE_URL='https://api.wodeschool.com'
VITE_BASE_PATH=''

View File

@ -15,6 +15,7 @@
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.2.2",
"dotenv": "^16.5.0",
"vite": "^6.3.1"
}
}

View File

@ -21,6 +21,9 @@ importers:
'@vitejs/plugin-vue':
specifier: ^5.2.2
version: 5.2.3(vite@6.3.2)(vue@3.5.13)
dotenv:
specifier: ^16.5.0
version: 16.5.0
vite:
specifier: ^6.3.1
version: 6.3.2
@ -413,6 +416,10 @@ packages:
resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==}
engines: {node: '>=0.4.0'}
dotenv@16.5.0:
resolution: {integrity: sha512-m/C+AwOAr9/W1UOIZUo232ejMNnJAJtYQjUbHoNTBNTJSvqzzDh7vnrei3o3r3m9blf6ZoDkvcw0VmozNRFJxg==}
engines: {node: '>=12'}
dunder-proto@1.0.1:
resolution: {integrity: sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==}
engines: {node: '>= 0.4'}
@ -917,6 +924,8 @@ snapshots:
delayed-stream@1.0.0: {}
dotenv@16.5.0: {}
dunder-proto@1.0.1:
dependencies:
call-bind-apply-helpers: 1.0.2

View File

@ -1,10 +1,10 @@
<script setup>
import axios from 'axios'
import request from './api/request.js'
import HelloWorld from './components/HelloWorld.vue'
const fetchData = async()=>{
try {
const response=await axios.get('api/wechat/token')
const response=await request.get('/wechat/token')
console.log(response.data)
} catch (error) {
console.error(error)

View File

@ -2,7 +2,7 @@ import axios from 'axios';
// 创建 axios 实例
const service = axios.create({
baseURL: 'https://api.wodeschool.com', // 替换为你的实际 baseURL
baseURL: import.meta.env.VITE_BASE_URL+import.meta.env.VITE_BASE_PATH,
timeout: 5000 // 请求超时时间
});
@ -43,6 +43,8 @@ service.interceptors.response.use(
// 封装请求方法
const request = {
get(url, params) {
console.log(process.env.NODE_ENV)
console.log(import.meta.env.VITE_BASE_URL,import.meta.env.VITE_BASE_PATH)
return service.get(url, { params });
},
post(url, data) {

View File

@ -1,7 +1,15 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import dotenv from 'dotenv'
// 加载环境变量
dotenv.config();
// 根据当前环境加载对应的 .env 文件
const env = dotenv.config({
path: `.env.${process.env.NODE_ENV}`
}).parsed;
// https://vite.dev/config/
export default defineConfig({
plugins: [vue()],
server:{