欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue cli4下环境变量和模式示例详解

程序员文章站 2022-05-27 18:50:02
本文介绍了vue cli4下环境变量和模式示例详解,分享给大家,具体如下:环境变量一个环境变量文件只包含环境变量的键值对:node_env=developmentvue_app_base_url=ht...

本文介绍了vue cli4下环境变量和模式示例详解,分享给大家,具体如下:

环境变量

一个环境变量文件只包含环境变量的键值对:

node_env=development
vue_app_base_url=http://127.0.0.1:8080/

注意:

  • node_env - 是 “development”、“production” 、"test"或者自定义的值。具体的值取决于应用运行的模式
  • base_url - 会和 vue.config.js 中的 publicpath 选项相符,即你的应用会部署到的基础路径
  • 除了 node_env 和 base_url,其他的环境变量必须以 vue_app_ 开头
  • 项目中使用:process.env.环境变量名,eg:vue_app_base_url

模式

模式是 vue cli 项目中一个重要的概念。默认情况下,一个 vue cli 项目有三个模式:

  • development 模式用于 vue-cli-service serve
  • production 模式用于 vue-cli-service build 和 vue-cli-service test:e2e
  • test 模式用于 vue-cli-service test:unit

注意点:

  • 一个模式可以包含多个环境变量
  • 每个模式都会将环境变量中 node_env 的值设置为模式的名称
  • 可以通过为 .env 文件增加后缀来设置某个模式下特有的环境变量
  • 为一个特定模式准备的环境文件 (例如 .env.production) 将会比一般的环境文件 (例如 .env) 拥有更高的优先级
  • 此外,vue cli 启动时已经存在的环境变量拥有最高优先级,并不会被 .env 文件覆写
.env        # 在所有的环境中被载入
.env.local     # 在所有的环境中被载入,但会被 git 忽略
.env.[mode]     # 只在指定的模式中被载入,优先级高于.env和.env.local
.env.[mode].local  # 只在指定的模式中被载入,但会被 git 忽略,优先级高于.env和.env.local

例子:不同模式下,为axios指定不同的baseurl

创建development模式的环境变量文件,项目根目录下新建.env.development文件

node_env=development
vue_app_base_url=http://127.0.0.1:8080/

创建production模式的环境变量文件,项目根目录下新建.env.production文件

node_env=production
vue_app_base_url=/

在src目录下main.js文件中使用环境变量

import vue from 'vue'
import app from './app.vue'

// 导入axios
import axios from 'axios'
// 设置请求根路径,使用环境变量
axios.defaults.baseurl = process.env.vue_app_base_url
// axios拦截器
axios.interceptors.request.use(config => {
 // 为请求头对象,添加token验证的authorization字段
 config.headers.authorization = window.sessionstorage.getitem('token')
 // 在最后必须return config
 return config
})
// 挂载到vue
vue.prototype.$http = axios

vue.config.productiontip = false

new vue({
 router,
 render: h => h(app)
}).$mount('#app')

也可以在其他vue组件中打印

console.log(process.env.node_env)
console.log(process.env.vue_app_base_url)
console.log(this.$http.defaults.baseurl)

运行项目

npm run serve

例子:自定义模式

自定义一个fat模式

在项目根目录下新建环境变量文件.env.fat

node_env=fat
vue_app_base_url=http://fat.com/

根目录下package.json中新增脚本命令

{
 "name": "vue_shop",
 "version": "0.1.0",
 "private": true,
 "scripts": {
  "serve": "vue-cli-service serve",
  "build": "vue-cli-service build",
  // 这条命令是我自定义的,通过--mode指定模式为fat
  "fat": "vue-cli-service serve --mode fat",
  "lint": "vue-cli-service lint"
 },
 "dependencies": {
  "axios": "^0.19.2",
  "core-js": "^3.4.4",
  "echarts": "^4.6.0",
  "element-ui": "^2.4.5",
  "vue": "^2.6.10",
  "vue-router": "^3.1.3"
 },
 "devdependencies": {
  "@vue/cli-plugin-babel": "^4.1.0",
  "@vue/cli-plugin-eslint": "^4.1.0",
  "@vue/cli-plugin-router": "^4.1.0",
  "@vue/cli-service": "^4.1.0",
  "@vue/eslint-config-standard": "^4.0.0",
  "babel-eslint": "^10.0.3",
  "babel-plugin-component": "^1.1.1",
  "eslint": "^5.16.0",
  "eslint-plugin-vue": "^5.0.0",
  "less": "^3.10.3",
  "less-loader": "^5.0.0",
  "vue-cli-plugin-element": "^1.0.1",
  "vue-template-compiler": "^2.6.10"
 }
}

运行命令

npm run fat

这时候项目中读取的,就是fat模式下的环境变量了

到此这篇关于vue cli4下环境变量和模式示例详解的文章就介绍到这了,更多相关vue cli4环境变量和模式内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!