≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

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

人气:411 时间:2019-04-11

这篇文章主要为大家详细介绍了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 接口前缀,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对四海网网站的支持!

本文来自:http://www.q1010.com/184/5408-0.html

注:关于vue 项目打包通过命令修改 vue-router 模式 修改 API 接口前缀的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • 分析vue-router 初始化时做了什么
  • 用vue快速开发app的脚手架工具
  • 解析vue路由异步组件和懒加载案例
  • Vue不能观察到数组length的变化
  • 浅谈Vue路由快照实现思路及其问题
  • 深入浅析Vue中的Prop
  • 使用Vue-cli 3.0搭建Vue项目的方法
  • vue 国际化 vue-i18n 双语言 语言包
  • vue指令只能输入正数并且只能输入一个小数点的方法
  • Vue.js 中取得后台原生HTML字符串 原样显示问题的解决方法
上一篇:Vue组件全局注册实现警告框的实例分析
下一篇:解析vue路由异步组件和懒加载案例
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。