How to manage the backend interface address in the Vue project built by Webpack

Source: Internet
Author: User

In the recently done Vue project, using the Webpack Packaging tool, previously in the project test environment and production environment interface address is the same, because the interface address is not the same, need to manually switch the different address when the project is packaged, sometimes forget to switch to RePack, back and forth is very troublesome, Today, online to see a good way to solve this problem, but also because of the Webpack tool is not very understanding, in fact, this tool has provided a solution.

Reference website: http://blog.csdn.net/gebitan505/article/details/58166055;

In the Config folder there are three JS files, respectively, Dev.env.js,index.js and Prod.env.js, respectively, representing the test environment configuration information, the main configuration information, production environment configuration information.

Open the Dev.env.js file; NODE_ENV Add an entry below with the following code:

var merge = require (' Webpack-merge ')var prodenv = require ('./prod.env '= Merge (prodenv, {  ' "Development" ',  ' "//192.168.1.8/api" '//interface of the test Environment })

Then, in the edit prod.env.js file, the code is as follows

Module.exports = {  ' "Production" ',  ' "//www.baidu.com/api" '//Interface for production environment }

Finally, the ROOT URL of the setting is changed to: Process.env.API_ROOT,

For example, let Rooturl = "http://test.api.com" is changed to let Rooturl = Process.env.API_ROOT;

Webpack in the local testing of the use of the test interface, packaging will automatically replace the interface with the interface of the production environment;

How to manage the backend interface address in the Vue project built by Webpack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.