vue使用process.env搭建自定义运行环境

一、vue-cli项目下默认有三种模式:

  • development:在 vue-cli-service serve 时使用。
  • production:在 vue-cli-service build 和 vue-cli-service test:e2e 时使用。
  • test:在 vue-cli-service test:unit 时使用。
    对应的 process.env.NODE_ENV 值分别为 development、production、test。

二、可以通过环境文件来指定默认环境变量和自定义环境变量,环境文件有一下几个类型:

  • .env: 在所有的环境中被载入
  • .env.local: 在所有的环境中被载入,但会被 git 忽略
  • .env.[mode]: 只在指定的模式中被载入
  • .env.[mode].local: 只在指定的模式中被载入,但会被 git 忽略

mode是某个模块名,如 在src创建 .env.friend 文件,内容:

NODE_ENV=development // 这里可以指定默认的环境是 development、production、test。
VUE_APP_ENV=friend  // 自定义的friend环境

注意自定义的变量名必须以 VUE_APP_ 开头才能被webpack.DefinePlugin 静态嵌入,通过process.env.VUE_APP_xxx 来访问;执行此文件就相当于“进入”friend环境了。

三、执行自定义环境文件,进入自定义环境

在package.json中添加

  "scripts": {"serve": "vue-cli-service serve","build": "vue-cli-service build","test:unit": "vue-cli-service test:unit","test:e2e": "vue-cli-service test:e2e","lint": "vue-cli-service lint","serve:f":"vue-cli-service serve --mode friend",},

执行 npm run serve:f,此时process.env.NODE_ENV为development, process.env.VUE_APP_ENV为friend,利用process.env.VUE_APP_ENV定义ip等信息即可,即可访问后台朋友的接口了。

四、配置举例和应用场景 vue-cli3

public/config/build.js,这里未使用自定义环境(未用process.env.VUE_APP_ENV)

let root = process.env.NODE_ENV;let build = {development: {//开发人员本地调试开发环境publicPath:"/",outputDir:"xuzhou_shuiwu_web",},production: {publicPath:"./",outputDir:"xuzhou_shuiwu_web",},test: {},// 公网环境pro: {},
};
// export default build[root];module.exports = build[root]

public/config/ip.js

// let root = process.env.NODE_ENV;
let root = process.env.VUE_APP_ENV; // 自定义
let key = "/back";// 代理关键字
// 通用配置
let common = {key: key,host: "0.0.0.0",port: "8888",localhost: "0.0.0.0:8888",upload: key + "/file/uploadFile", // 文件上传地址
}
let ipConfig = {// 默认环境development: {serverIP: "http://1.1.1.1:8883/portal/",  logoutIp: "http://1.1.1.1:8883/portal/cas/logout/",},// 正式环境production: {serverIP: "http://1.1.1.1:8082/portal/", logoutIp: "http://1.1.1.1:8082/portal/cas/logout/",},// 测试环境test: {},// 自定义环境friend: {serverIP: "http://1.1.1.1:8881/portal/",logoutIp: "http://1.1.1.1:8881/portal/cas/logout/",},
};// export default Object.assign(common,ipConfig[root]);
console.log("当前环境:",root)
module.exports = Object.assign(common,ipConfig[root])

public/config/index.js

// import ip from "./ip"
// import build from "./build"const config = {ip: require("./ip"), build: require("./build")
};
// export default config;module.exports = config

vue.config.js

const config = require('./public/config');
const path = require("path");function resolve(dir) {return path.join(__dirname, dir);
}module.exports = {// publicPath: "./", //打包后的位置(如果不设置这个静态资源会报404) ./// vue-cli 3 已废弃baseUrlpublicPath: config.build.publicPath,outputDir: config.build.outputDir, //打包后的目录名称assetsDir: "static", //静态资源目录名称devServer: {open: true, // disableHostCheck: false,host: config.ip.host,port: config.ip.port,// https: false,// hotOnly: false, // See https://github.com/vuejs/vue-cli/blob/dev/docs/cli-service.md#configuring-proxyproxy: {[config.ip.key]: {target: config.ip.serverIP,  // jenkinschangeOrigin: true,pathRewrite: {["^" + config.ip.key]: ""}}}// before: app => {}}, // 第三方插件配置// webpack相关配置// chainWebpack: config => {//   // config.entry.app = ['./src/main.js'];//   config.resolve.alias.set("@", resolve("src")).set("#", resolve("public"));// },// webpack3,4的写法configureWebpack: {resolve: {extensions: ['.js', '.vue', '.json'],alias: {'@': resolve('src'),// '~': process.cwd(),'#': resolve('public'),// components: resolve('src/components'),// util: resolve('src/utils'),// store: resolve('src/store'),// router: resolve('src/router')}}},// pluginOptions: {//     // ...// },// 加载less加载器,路径:./public/css/common.lesschainWebpack: config => {const oneOfsMap = config.module.rule("less").oneOfs.store;oneOfsMap.forEach(item => {item.use("sass-resources-loader").loader("sass-resources-loader").options({// Provide path to the file with resourcesresources: ["./public/css/common.less", "./public/css/variable.less"]}).end();});}
};

axure封装request.js 部分

const service = axios.create({baseURL: require("#/config").ip.key,timeout: 5000, // request timeoutwithCredentials: true,
});

在单点登录中,可以在router.js中判断无权限跳转时使用:
location.href = config.serverIP + “cas/login?redirect=” + url

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278602.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

bootstrap评分插件 Bootstrap Star Rating Examples

http://www.jq22.com/demo/bootstrap-star-rating-master201708041812/ 转载于:https://www.cnblogs.com/waw/p/8288951.html

http 请求报文

1、报文 2、http请求方法 restful接口 post:创建 put:更新 转载于:https://www.cnblogs.com/mengfangui/p/10171559.html

chrome硬件加速_如何在Chrome中打开和关闭硬件加速

chrome硬件加速Google Chrome comes equipped with hardware acceleration, a feature which takes advantage of your computer’s GPU to speed up processes and free vital CPU time. However, sometimes driver incompatibilities can cause this feature to misbehave an…

春节您“抢票”到手了吗,如果没,请进来看看!

不是为了卖“广告”!我与软件作者从不认识!我与软件作者因为抢票认识,不,只认识他写的软件!51CTO博客2.0后,我一直没有写博文!主要原因:不能用Live Writer写博文,复制,粘…

两个矩阵相加 Exercise08_05

1 import java.util.Scanner;2 /**3 * author 冰樱梦4 * 时间:2018年12月5 * 题目:两个矩阵相加6 *7 */8 public class Exercise08_05 {9 public static void main(String[] args){ 10 Scanner inputnew Scanner(System.in); 11 …

vue element form中input等组件不能输入值

<el-input v-model"form.inputVal " />由于data中form只是一个空对象{}&#xff0c;当主动设置 form.inputVal “” 后input却仍无法输入值&#xff0c;这是因为inputVal 属性没有get和set&#xff0c;需要用vue内置属性设置&#xff1a;this.$set(this.form,…

如何在PowerPoint中制作三折

While Microsoft PowerPoint is almost exclusively used for presentation purposes, it’s also a great application for creating interesting and visually appealing brochures. Here’s how to create (and print out) a tri-fold using PowerPoint. 尽管Microsoft Powe…

彻底理解数据库事物

事务 事务(Transaction)&#xff0c;一般是指要做的或所做的事情。在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit)。在计算机术语中&#xff0c;事务通常就是指数据库事务。 概念 一个数据库事务通常包含对数据库进行读或写的一个操作序列。它的…

HttpRunner自动化框架学习笔记

一.简单介绍 HttpRunner 是一款面向 HTTP(S) 协议的通用测试框架&#xff0c;只需编写维护一份 YAML/JSON 脚本&#xff0c;即可实现自动化测试、性能测试、线上监控、持续集成等多种测试需求。 支持python2和python3 二.框架特点 继承 Requests 的全部特性&#xff0c;轻松实现…

如何在Chrome中为Gmail启用桌面通知

Last year Google rolled out desktop notifications for Google Calendar, now you can get Gmail and Gchat notifications on your desktop too. Read on as we walk you through configuring them both. 去年Google推出了Google日历的桌面通知&#xff0c;现在您也可以在桌…

vue集成iconfont、fontawesome和图标选择器(含fontawesome、el-icon和加入的iconfont)

目录&#xff08;一&#xff09;引入iconfont字体图标库将图标加入购物车新建&#xff08;添加至&#xff09;项目下载后项目中引入&#xff08;二&#xff09;引入fontawesome&#xff08;三&#xff09;图标选择器效果图结构使用源码&#xff08;一&#xff09;引入iconfont字…

java之Synchronize

2019独角兽企业重金招聘Python工程师标准>>> 实现原理&#xff1a;JVM 是通过进入、退出对象监视器( Monitor )来实现对方法、同步块的同步的。 具体实现是在编译之后在同步方法调用前加入一个 monitor.enter 指令&#xff0c;在退出方法和异常处插入 monitor.exit …

pop()方法

pop()方法 描述 列表 pop() 方法通过指定元素的索引值来移除列表中的某个元素&#xff08;默认是最后一个元素&#xff09;&#xff0c;并且返回该元素的值&#xff0c;如果列表为空或者索引值超出范围会报一个异常。 语法 pop() 方法语法&#xff1a; L.pop([index-1]) 参数 i…

vue引入postcss-plugin-px2rem,px转rem

npm install --save-dev postcss-plugin-px2remvue.config.js module.exports {css: {loaderOptions: {postcss: {plugins: [require(postcss-plugin-px2rem)({rootValue: 16, //换算基数&#xff0c; 默认100 &#xff0c;1 / fontsize(html) x 原来的1px转为0.0625rem// …

HikariCP连接池配置

2019独角兽企业重金招聘Python工程师标准>>> HikariCP号称性能最好的Java数据库连接池。虽没做过亲测但是公司项目一直在用&#xff0c;大概经历过2万左右用户同时在线&#xff0c;链接池性能方面未出现问题。 官网&#xff1a;http://brettwooldridge.github.io/Hi…

Microsoft Desktop Player是IT Pro的宝贵工具

If you are an IT Professional, a new education tool introduced by Microsoft is the MS Desktop Player. Today we take a look at what it has to offer, from Webcasts, White Papers, Training Videos, and more. 如果您是IT专业人员&#xff0c;则Microsoft推出的新的培…

如何在Microsoft Excel中将文本转换为日期值

Analysis of business data often requires working with date values in Excel to answer questions such as “how much money did we make today” or “how does this compare to the same day last week?” And that can be hard when Excel doesn’t recognize the valu…

git针对已有仓库或已有文件的初始化操作

git全局配置用户 git config --global user.name “xxx” git config --global user.email “xxx.cn” 情况1、存在git仓库 git clone url.git //cd xx //touch README.md //git add README.md //git commit -m "add README" //git push -u origin master情况2、已…

策略模式-Golang实现

目的&#xff1a;根据不同策略来执行对象的相应操作 和工厂模式很像&#xff0c;不同点在于&#xff1a; 工厂模式是传入参数后创建对象&#xff0c;根据传入的参数写逻辑来判断应该创建什么类型的对象&#xff0c;模式的使用者调用对象统一的方法操作。 策略模式是模式的使用者…

看着手机会让您晕眩吗? 禁用动画

Giulio_Fornasar/ShutterstockGiulio_Fornasar /快门Are your phone’s buttery-smooth animations causing motion sickness, eyestrain, or even slow app performance? Those animations are just for looks, and you can disable a lot of them on both iPhone and Androi…