vue 项目打包通过命令修改 vue-router 模式 修改 API 接口前缀
需求说明:
在开发 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 接口前缀,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:
- 通过cordova将vue项目打包为Android app
- 16.如何做到webpack打包vue项目后,可以修改配置文件
- 详解vue项目打包后通过百度的BAE发布到网上的流程
- 修改后的specification模式c++实现,去掉了接口名的I前缀
- webpack+vue打包之后输出配置文件修改接口文件
- vue2.x 通过后端接口代理,获取qq音乐api的数据
- 第十六节: EF的CodeFirst模式通过Fluent API修改默认协定
- vue +vue-cli+vue router ,npm run build 打包完页面空白,不显示URL的#号router模式为history
- vue项目通过webpack打包生成的dist文件放到express里边运行(vue+webpack+express)
- 通过DOS、SHELL批处理命令加载Lib并编译和打包Java项目(或者运行项目)
- 通过程序调用tomcat的manager的text模式的命令来自动部署项目
- Vue webapp项目通过HBulider打包原生APP(vue+webpack+HBulider)
- webpack+vue-cil 配置接口地址代理以及将项目打包到子目录的方法
- 通过cordova将vue项目打包为webapp
- 【Vue】项目打包的时候,build.js文件过大,可以通过设置懒加载或者按照路由分模块加载进行打包
- 通过cordova将vue项目打包成app
- 已配置好的vue全家桶项目router,vuex,api,axios,vue-ls,async
- Vue.js项目API、Router配置拆分实践
- 详解Vue.js项目API、Router配置拆分实践
- vue和webpack打包项目相对路径修改的方法