...

by Zengjing 2019-03-22

Vue开发札记:构建后配置化与新开页面

构建后支持配置化修改

基于Vue的项目开发中,构建打包之后需要支持修改后端接口地址。

解决思路是采用window全局对象注入变量。

在static文件夹中定义全局变量config.js

window.gobal = {
  apiBaseUrl: "http://192.168.1.110:7999",
  requestTimeout: 30000,
  requestHeader: {
    'Content-Type': 'application/json-patch+json',
    'X-Requested-With': 'XMLHttpRequest'
  }
}

index.html文件引入config.js。为了便捷,可以注册成Vue全局属性。

Vue.prototype.$apiBaseUrl = window.gobal.apiBaseUrl;

这样在组件中就直接使用了。

如何新开一个页面

基于SPA构建的Vue,路由route的引入,替换了原来页面的概念。在没有页面的前提下,如何新开一个页面窗口呢?

解决思路是模拟 a 标签触发新开一个页面。

let a = this.$refs.previewMap;
let url = http://${window.location.host}/#/ol/${this.$route.params.uuid};
a.setAttribute("href", url);
a.click();

收获点赞: 0

评论

...