vue 项目打包通过命令修改 vue-router 模式 修改 API 接口前缀

作者:Miss_Liang 时间:2024-05-05 09:08:16 

需求说明:

在开发 vue 项目的过程中遇到的需求是要把 api 接口前缀暴露在命令行,通过 npm run build apiUrl 即可修改接口入口,用于从 docker 部署到不同的测试服务器上,其次是路由模式的问题,部署到测试服务器上的需要是 history 模式,但是产品是用 electron + vue 开发的桌面应用,electron 硬性要求 vue-router 的路由模式是 hash 模式,所以命令行需新增一个配置项 mode ,mode 可选值有 history 、hash

最终结果:

npm run build '' hash  --->  使用源码中写死的 api 入口 ,vue-router 模式是 hash 模式

npm run build https://192.168.166.101:8444 history  --->  使用 https://192.168.166.101:8444 作为 api 入口,vue-router 模式是 history 模式

实现:

1.新建 base/config.js 用于存放从 webpack.prod.conf.js 里写入的数据

2.新建 base/index.js 用于将从 base/config.js 里导出的 config 挂载在 Vue 原型的 $config 对象上

3.新建 build/apiConfig.js 用于封装 fs-extra 对文件的读写

4.在 webpack.prod.conf.js 将命令行中敲入的命令写入 base/config.js 里

5.在 main.js 中将 base/index.js 抛出的 install 挂载到 Vue 上

6.在 Login.vue 和 router/index.js 里引入 this.$config.host 以及 base/config.js 即可

关键代码:

2.新建 base/index.js 用于将从 base/config.js 里导出的 config 挂载在 Vue 原型的 $config 对象上


// 将 config 封装成插件
// example this.$config
// 导入所有接口
import config from './config';
const install = Vue => {
 if(install.installed)
   return;
 install.installed = true;
 Object.defineProperties(Vue.prototype, {
   // 此处挂载在 Vue 原型的 $config 对象上
   $config:{
     get(){
       return config;
     }
   }
 })
}
export default install;

3.新建 build/apiConfig.js 用于封装 fs-extra 对文件的读写


const fs = require("fs-extra");
const path = require("path");
var _path = path.join(__dirname, "../src/base/host.js");
if (!fs.pathExistsSync(_path)) {
// 如果不存在路径
fs.mkdirpSync(_path); // 就创建
}
module.exports = {
read: function() {
 let filesData = fs.readFileSync(_path, "utf-8", function(e, data) {
  if (e) throw e;
  return data;
 });
 return filesData;
},
write: function(writeStr) {
 fs.open(_path, "w", function(e, fd) {
  if (e) throw e;
  fs.write(fd, writeStr, 0, "utf8", function(e) {
   if (e) throw e;
   fs.closeSync(fd);
  });
 });
}
};

4.在 webpack.prod.conf.js 将命令行中敲入的命令写入 base/config.js 里  


const apiConfig = require('./apiConfig');
apiConfig.read();
apiConfig.write(
`export const host = '${process.argv[2]}';
 export const mode = '${process.argv[3]}';
// 默认全部倒出
// 根绝需要进行  
export default {
host,
mode
}`
);

5.在 main.js 中将 base/index.js 抛出的 install 挂载到 Vue 上


import host from './base/index';
Vue.use(host);

6.在 Login.vue 里引入 this.$config.host


this.$store.set("presetPort", this.$config.host ? this.$config.host.split(":")[2] : "443"); // 设置预置端口
this.$store.set("presetHost", this.$config.host ? this.$config.host.split(":")[0] : "https"); // 设置预置协议
this.$store.set("presetIP", this.$config.host ? this.$config.host.split(":")[1].split("/")[2] : "192.168.166.109"); // 设置预置IP

6.在 router/index.js 里引入 base/config.js


import { mode } from '@/base/config';
let router = null;
let routes = [
{
 path: 'xxx',
 name: 'xxx',
 component: xxx
}...];
mode === 'history' ? routes.push({path:"*",component:xxx}) : "";
router = new Router({
mode: mode,
routes:routes
})
export default router;

总结

以上所述是小编给大家介绍的vue 项目打包通过命令修改 vue-router 模式 修改 API 接口前缀网站的支持!

来源:https://blog.csdn.net/Miss_Liang/article/details/80676395

标签:vue,router,打包,模式
0
投稿

猜你喜欢

  • Go获取与设置环境变量的方法详解

    2023-06-21 04:35:10
  • ini_set的用法介绍

    2023-11-15 07:31:56
  • Echarts图表移动端横屏进入退出的实现

    2024-05-11 09:06:45
  • mysql中update按照多重条件进行更新处理的方案

    2024-01-22 05:14:59
  • keras实现VGG16 CIFAR10数据集方式

    2023-08-19 08:27:08
  • 从零开始实现Vue简单的Toast插件

    2024-05-13 09:13:55
  • Oracle PL/SQL入门案例实践

    2010-07-18 13:13:00
  • vue 使用vant插件做tabs切换和无限加载功能的实现

    2024-04-10 13:49:34
  • JavaScript 回车 焦点切换

    2024-04-17 10:22:53
  • go语言处理TCP拆包/粘包的具体实现

    2023-08-25 12:19:00
  • 对“关于购物车的想法”的一些回复

    2009-03-10 18:15:00
  • javascript中select下拉框的用法总结

    2024-04-19 09:57:51
  • 使用Python开发游戏运行脚本成功调用大漠插件

    2021-03-09 21:05:53
  • Mysql安装与配置调优及修改root密码的方法

    2024-01-15 19:35:06
  • Vue+Element自定义纵向表格表头教程

    2023-07-02 17:10:38
  • JavaScript实现动态数字时钟

    2024-04-10 11:01:09
  • 用户体验量化方法研究(二)

    2010-02-08 12:46:00
  • Flask web开发处理POST请求实现(登录案例)

    2022-03-06 09:34:13
  • python获取全国城市pm2.5、臭氧等空气质量过程解析

    2023-06-04 21:46:07
  • 如何在Python中用好短路机制

    2022-04-23 16:56:42
  • asp之家 网络编程 m.aspxhome.com