Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate a configuration file after vue packaging

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report