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

2020-06-16 06:58:30易采站长站整理

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环境变量和模式内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!