file-type

Vue 3.0 环境变量配置及axios封装实战指南

下载需积分: 10 | 132KB | 更新于2025-01-24 | 4 浏览量 | 0 下载量 举报 收藏
download 立即下载
### Vue 3.0 环境变量配置详解 #### 1. Vue 3.0 环境变量的基本概念 环境变量是一种在操作系统中用来设置环境信息的变量,它可以在运行时为程序提供不同的配置选项。在前端项目中,我们通常会根据运行环境的不同(如本地开发环境、测试环境、生产环境)来配置不同的环境变量,从而实现对项目的不同配置。 #### 2. Vue 项目环境变量配置的实现方式 在 Vue.js 项目中,环境变量的配置通常涉及以下几个方面: - 在项目根目录下创建或修改环境配置文件(例如 `.env.development` 用于开发环境)。 - 通过 `.env` 文件为不同环境设置不同的环境变量。 - 使用 `vue.config.js` 配置文件来自定义项目的打包配置。 - 使用 `package.json` 或 `package-lock.json` 等文件来管理项目依赖和版本。 #### 3. Vue 3.0 配置文件详解 - **.browserslistrc**: 此文件用于指定项目的目标浏览器范围,这对于自动选择符合当前环境的浏览器兼容性配置非常有用,例如在不同浏览器中使用的特性查询。 - **.env.development**: 此文件通常包含开发环境下的环境变量设置。例如,可以在此文件中设置 `VUE_APP_BASE_URL` 为开发环境的API基础地址。 - **.gitignore**: 用于定义不希望提交到Git仓库的文件和目录,比如本地配置文件和node模块文件等。 - **vue.config.js**: Vue CLI 生成的配置文件,可以在此文件中自定义webpack配置,比如设置跨域代理、修改输出目录等。 - **.eslintrc.js**: 配置ESLint规则的文件,可以针对项目设置具体的语法检查规则。 - **babel.config.js**: Babel的配置文件,用于指定如何转译JavaScript代码,以便于向后兼容不同版本的浏览器。 - **package-lock.json** 和 **package.json**: 前者是npm包管理器的锁定文件,确保所有开发者的依赖环境一致,后者定义了项目的元数据、依赖及脚本命令。 - **yarn.lock**: 与package-lock.json类似,它是yarn包管理器的锁定文件。 - **README.md**: 项目文档文件,通常包含项目的安装指南、使用方法、配置信息等。 #### 4. Vue 3.0 环境变量配置操作流程 - **创建和修改配置文件**: - 复制 `.env` 文件的模板(例如 `.env.template`),并根据实际环境重命名为 `.env.development` 或 `.env.production`。 - 修改相应配置文件,如在 `.env.development` 中设置 `VUE_APP_BASE_URL=http://localhost:3000`。 - **封装 axios**: - 通过读取环境变量,动态获取不同环境下的API基础地址,并在axios中设置。 - 根据当前环境,选择对应的环境变量进行API请求的URL拼接。 - 示例代码: ```javascript import axios from 'axios'; import envConfig from './path/to/env/config'; const instance = axios.create({ baseURL: envConfig.VUE_APP_BASE_URL, }); export default instance; ``` - **模块化开发**: - 将项目拆分成独立的、可复用的模块。 - 每个模块负责一部分功能,并通过import/export的方式导入导出。 - 利于维护和测试,也方便代码的复用。 - **代码测试**: - 在实际项目中,使用单元测试或端到端测试来确保代码的正确性和稳定性。 - 可以通过Jest、Mocha等测试框架来编写测试用例,并检查环境配置是否正确应用。 #### 5. Vue 3.0 环境变量配置的注意事项 - 确保不要将敏感信息(如API密钥、密码)直接存放在版本控制中。 - 不同环境的配置文件应避免冲突,并确保在切换环境时使用正确的配置文件。 - 环境变量命名应遵循一定规范,推荐使用 `VUE_APP_` 前缀,因为这样的环境变量才会被 `process.env` 正确加载。 - 使用合适的脚本命令来启动项目,如 `npm run serve` 和 `npm run build` 分别对应开发环境和生产环境的启动命令。 #### 总结 在Vue 3.0项目中,通过上述的配置文件和操作流程,我们可以实现一个灵活的环境变量配置机制,从而使得项目能够快速适应不同的开发、测试和生产环境。通过环境变量的合理配置和管理,可以显著提高开发效率,加强项目的安全性和可维护性。

相关推荐

filetype

切换华为云镜像后为什么有些还是淘宝的镜像PS D:\project-sky-admin-vue-ts2> npm install https://mirrors.huaweicloud.com/repository/npm/element-ui/-/element-ui-2.13.0.tgz --legacy-peer-deps --verbose npm verbose cli D:\桌面\node.exe D:\桌面\node_modules\npm\bin\npm-cli.js npm info using [email protected] npm info using [email protected] npm verbose title npm install https://mirrors.huaweicloud.com/repository/npm/element-ui/-/element-ui-2.13.0.tgz npm verbose argv "install" "https://mirrors.huaweicloud.com/repository/npm/element-ui/-/element-ui-2.13.0.tgz" "--legacy-peer-deps" "--loglevel" "verbose" npm verbose logfile logs-max:10 dir:C:\Users\ASUS\AppData\Local\npm-cache\_logs\2025-07-29T03_54_53_483Z- npm verbose logfile C:\Users\ASUS\AppData\Local\npm-cache\_logs\2025-07-29T03_54_53_483Z-debug-0.log npm http fetch GET 200 https://mirrors.huaweicloud.com/repository/npm/element-ui/-/element-ui-2.13.0.tgz 1520ms (cache miss) npm http fetch GET 200 https://mirrors.huaweicloud.com/repository/npm/@types%2fecharts 122ms (cache updated) npm http fetch GET 200 https://mirrors.huaweicloud.com/repository/npm/@types%2fwebpack 270ms (cache updated) npm http fetch GET 200 https://mirrors.huaweicloud.com/repository/npm/area-data 72ms (cache updated) npm http fetch GET https://registry.npm.taobao.org/axios/download/axios-0.19.0.tgz?cache=0&other_urls=https%3A%2F%2Fsiteproxy.ruqli.workers.dev%3A443%2Fhttps%2Fregistry.npm.taobao.org%2Faxios%2Fdownload%2Faxios-0.19.0.tgz attempt 1 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/axios/download/axios-0.19.0.tgz?cache=0&other_urls=https%3A%2F%2Fsiteproxy.ruqli.workers.dev%3A443%2Fhttps%2Fregistry.npm.taobao.org%2Faxios%2Fdownload%2Faxios-0.19.0.tgz attempt 2 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/axios/download/axios-0.19.0.tgz?cache=0&other_urls=https%3A%2F%2Fsiteproxy.ruqli.workers.dev%3A443%2Fhttps%2Fregistry.npm.taobao.org%2Faxios%2Fdownload%2Faxios-0.19.0.tgz attempt 3 failed with CERT_HAS_EXPIRED npm http fetch GET 200 https://mirrors.huaweicloud.com/repository/npm/echarts 586ms (cache updated) npm http fetch GET 200 https://mirrors.huaweicloud.com/repository/npm/element-ui/-/element-ui-2.13.0.tgz 38ms (cache hit) npm http fetch GET https://registry.npm.taobao.org/js-cookie/download/js-cookie-2.2.1.tgz attempt 1 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/js-cookie/download/js-cookie-2.2.1.tgz attempt 2 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/js-cookie/download/js-cookie-2.2.1.tgz attempt 3 failed with CERT_HAS_EXPIRED npm http fetch GET 200 https://mirrors.huaweicloud.com/repository/npm/md5 918ms (cache miss) npm http fetch GET https://registry.npm.taobao.org/moment/download/moment-2.24.0.tgz attempt 1 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/moment/download/moment-2.24.0.tgz attempt 2 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/moment/download/moment-2.24.0.tgz attempt 3 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/normalize.css/download/normalize.css-8.0.1.tgz attempt 1 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/normalize.css/download/normalize.css-8.0.1.tgz attempt 2 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/normalize.css/download/normalize.css-8.0.1.tgz attempt 3 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/nprogress/download/nprogress-0.2.0.tgz attempt 1 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/nprogress/download/nprogress-0.2.0.tgz attempt 2 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/nprogress/download/nprogress-0.2.0.tgz attempt 3 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/path-to-regexp/download/path-to-regexp-3.2.0.tgz attempt 1 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/path-to-regexp/download/path-to-regexp-3.2.0.tgz attempt 2 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/path-to-regexp/download/path-to-regexp-3.2.0.tgz attempt 3 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/register-service-worker/download/register-service-worker-1.6.2.tgz attempt 1 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/register-service-worker/download/register-service-worker-1.6.2.tgz attempt 2 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/register-service-worker/download/register-service-worker-1.6.2.tgz attempt 3 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/vue/download/vue-2.6.10.tgz attempt 1 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/vue/download/vue-2.6.10.tgz attempt 2 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/vue/download/vue-2.6.10.tgz attempt 3 failed with CERT_HAS_EXPIRED npm http fetch GET 200 https://mirrors.huaweicloud.com/repository/npm/vue-area-linkage 542ms (cache miss) npm http fetch GET https://registry.npm.taobao.org/vue-class-component/download/vue-class-component-7.1.0.tgz attempt 1 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/vue-class-component/download/vue-class-component-7.1.0.tgz attempt 2 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/vue-class-component/download/vue-class-component-7.1.0.tgz attempt 3 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/vue-property-decorator/download/vue-property-decorator-8.3.0.tgz attempt 1 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/vue-property-decorator/download/vue-property-decorator-8.3.0.tgz attempt 2 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/vue-property-decorator/download/vue-property-decorator-8.3.0.tgz attempt 3 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/vue-router/download/vue-router-3.1.3.tgz?cache=0&sync_timestamp=1572571382028&other_urls=https%3A%2F%2Fsiteproxy.ruqli.workers.dev%3A443%2Fhttps%2Fregistry.npm.taobao.org%2Fvue-router%2Fdownload%2Fvue-router-3.1.3.tgz attempt 1 failed with CERT_HAS_EXPIRED npm http fetch GET https://registry.npm.taobao.org/vue-router/download/vue-router-3.1.3.tgz?cache=0&sync_timestamp=1572571382028&other_urls=https%3A%2F%2Fsiteproxy.ruqli.workers.dev%3A443%2Fhttps%2Fregistry.npm.taobao.org%2Fvue-router%2Fdownload%2Fvue-router-3.1.3.tgz attempt 2 failed with CERT_HAS_EXPIRED \

filetype
filetype
filetype

F:\myProject\ele\dist>npm install --save-dev rimraf npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: [email protected] npm ERR! Found: [email protected] npm ERR! node_modules/stylelint npm ERR! peer stylelint@">=14.0.0" from [email protected] npm ERR! node_modules/stylelint-config-html npm ERR! stylelint-config-html@">=1.0.0" from [email protected] npm ERR! node_modules/stylelint-config-recommended-vue npm ERR! stylelint-config-recommended-vue@">=1.1.0" from [email protected] npm ERR! node_modules/stylelint-config-standard-vue npm ERR! dev stylelint-config-standard-vue@"^1.0.0" from the root project npm ERR! stylelint-config-html@">=1.0.0" from [email protected] npm ERR! node_modules/stylelint-config-standard-vue npm ERR! dev stylelint-config-standard-vue@"^1.0.0" from the root project npm ERR! peer stylelint@">=16" from [email protected] npm ERR! node_modules/stylelint-config-recess-order npm ERR! dev stylelint-config-recess-order@"^6.0.0" from the root project npm ERR! 11 more (stylelint-config-recommended, ...) npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer stylelint@">= 11.x < 15" from [email protected] npm ERR! node_modules/stylelint-config-prettier npm ERR! dev stylelint-config-prettier@"^9.0.5" from the root project npm ERR! npm ERR! Conflicting peer dependency: [email protected] npm ERR! node_modules/stylelint npm ERR! peer stylelint@">= 11.x < 15" from [email protected] npm ERR! node_modules/stylelint-config-prettier npm ERR! dev stylelint-config-prettier@"^9.0.5" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! For a full report see: npm ERR! C:\Users\Administrator\AppData\Local\npm-cache\_logs\2025-05-12T03_10_57_261Z-eresolve-report.txt npm ERR! A complete log of this run can be found in: C:\Users\Administrator\AppData\Local\npm-cache\_logs\2025-05-12T03_10_57_261Z-debug-0.log

filetype
filetype

{ "name": "vue-typescript-admin-template", "version": "0.1.0", "private": true, "author": "Chong Guo <[email protected]>", "scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "build:uat": "vue-cli-service build --mode production.uat", "lint": "vue-cli-service lint", "svg": "vsvg -s ./src/icons/svg -t ./src/icons/components --ext ts --es6", "test:e2e": "vue-cli-service test:e2e", "test:unit": "vue-cli-service test:unit" }, "dependencies": { "@types/echarts": "^4.4.6", "@types/webpack": "^4.41.12", "area-data": "^5.0.6", "axios": "^0.19.0", "echarts": "^5.3.2", "element-ui": "^2.12.0", "js-cookie": "^2.2.1", "md5": "^2.3.0", "moment": "^2.24.0", "normalize.css": "^8.0.1", "nprogress": "^0.2.0", "path-to-regexp": "^3.0.0", "register-service-worker": "^1.6.2", "vue": "^2.6.10", "vue-area-linkage": "^5.1.0", "vue-class-component": "^7.1.0", "vue-property-decorator": "^8.2.2", "vue-router": "^3.1.2", "vue-svgicon": "^3.2.6", "vuex": "^3.1.1", "vuex-class": "^0.3.2", "vuex-module-decorators": "^0.10.1", "vuex-persistedstate": "^2.7.0" }, "devDependencies": { "@types/jest": "^24.0.18", "@types/js-cookie": "^2.2.2", "@types/nprogress": "^0.2.0", "@types/webpack-env": "^1.14.0", "@vue/cli-plugin-babel": "^3.11.0", "@vue/cli-plugin-e2e-cypress": "^3.11.0", "@vue/cli-plugin-eslint": "^3.11.0", "@vue/cli-plugin-pwa": "^3.11.0", "@vue/cli-plugin-typescript": "^3.11.0", "@vue/cli-plugin-unit-jest": "^3.11.0", "@vue/cli-service": "^3.11.0", "@vue/eslint-config-standard": "^4.0.0", "@vue/eslint-config-typescript": "^4.0.0", "@vue/test-utils": "^1.0.0-beta.29", "babel-core": "^7.0.0-bridge.0", "babel-eslint": "^10.0.3", "eslint": "^6.2.2", "eslint-plugin-vue": "^5.2.3", "fibers": "^4.0.2", "jest": "^24.9.0", "sass": "^1.22.10", "sass-loader": "^7.3.1", "style-resources-loader": "^1.2.1", "ts-jest": "^24.0.2", "typescript": "3.6.2", "vue-cli-plugin-element": "^1.0.1", "vue-cli-plugin-style-resources-loader": "^0.1.3", "vue-template-compiler": "^2.6.10", "webpack": "^4.39.3" } } PS D:\My JAVA\苍穹外卖\前端源码\苍穹外卖前端源码\project-sky-admin-vue-ts> npm install npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: [email protected] npm ERR! Found: vue@undefined npm ERR! node_modules/vue npm ERR! vue@"^2.6.10" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer vue@">=2.2.0" from [email protected] npm ERR! node_modules/vue-area-linkage npm ERR! vue-area-linkage@"^5.1.0" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force, or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! See C:\Users\liuchaokun\AppData\Local\npm-cache\eresolve-report.txt for a full report. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\liuchaokun\AppData\Local\npm-cache\_logs\2025-07-28T03_08_03_535Z-debug-0.log

filetype
好了来看下一题
  • 粉丝: 5858
上传资源 快速赚钱