Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to generate a configuration file after vue packaging

Shulou Source: shulou.com Published: 2022-05-31 12:08:29 10月19日 Update

This article introduces the knowledge of "how to generate a configuration file after vue packaging". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Foreword:

After our vue code is packaged and uploaded to the server, if the domain name of the data interface is changed later, do we need to go to the vue file to modify the interface?

Can you generate a configuration file in which you can configure the domain name or other fields, so that if you change the domain name later, you just need to open notepad and modify the domain name.

Tutorial:

Step 1: install the generate-asset-webpack-plugin plug-in

Npm install-save-dev generate-asset-webpack-plugin

Step 2: configure the webpack.prod.conf.js file

/ / output the configurable file var GenerateAssetPlugin = require ('generate-asset-webpack-plugin'); var createServerConfig = function (compilation) {let cfgJson= {ApiUrl: "http://198.129.31.108:8080"}; return JSON.stringify (cfgJson) when packaging } / / enter a configurable file when packing / / add this code to plugins: [] new GenerateAssetPlugin ({filename: 'serverconfig.json', fn: (compilation, cb) = > {cb (null, createServerConfig (compilation));}, extraFiles: []})

Step 3: enter the npm run build packaging code and the result is as follows

Step 4: you need to modify the domain name in the future, and you can modify it in serverconfig.json.

Step 5: get ApiUrl

/ define a global function Vue.prototype.getConfigJson=function () {this.$http.get ("serverconfig.json") .then ((result) = > {/ / ApiUrl can also be stored in sessionStorage}) in main.js. Catch ((error) = > {console.log (error)});}

Step 6: use ApiUrl

/ / execute this.getConfigJson () in app.vue; mounted:function () {this.getConfigJson ();} / / after. Used where needed because ApiUrl is global, you can directly use this.ApiUrlvar url=this.ApiUrl+'/api/.... "how to generate a configuration file after vue packaging" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: File configuration domain name generation code global content field interface time more knowledge input output practical successful learning next function preface just need Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple macOS OPPO Reno vpn Linux