The Vue CLI Scaffolding Project uses Webpack to configure different interface addresses or different variable values for the production and release environments.

Source: Internet
Author: User

Not much nonsense to say, directly into the topic, this article to configure a different interface domain name address as an example

There is a config folder under the project root, which includes at least three files in the base project.

1.dev.env.js

2.index.js

3.prod.env.js

What we need to do is configure the first and the third one.

In fact, these two file content is to set different parameters for the production environment and publishing environment files, then open dev.en.js, development environment. The original code is as follows:

' Use strict ' const merge = require (' webpack-merge ') const PRODENV = require ('./prod.env ') module.exports = Merge (Prodenv, {  node_env: ' "Development" ',})

Then we configure a BASE_API, add a line of code on the page, as follows

' Use strict ' const merge = require (' webpack-merge ') const PRODENV = require ('./prod.env ') module.exports = Merge (Prodenv, {  node_env: ' Development ',  Base_api: ' "https://www.baidu.com" ',})

Then, we edit the Prod.env.js file, the production environment, the original code is as follows

' Use strict ' Module.exports = {  node_env: ' "Production" ',}

On the basis of this file, add a line of code, changed as follows

' Use strict ' Module.exports = {  node_env: ' "Production" ',  Base_api: ' "https://www.baidu.com" ',}

At last

Configure API interface address var root = Process.env.BASE_API

This time root is the common base variable, you can directly access the interface when this write: root+ "Api/home" and this variable will be automatically adjusted according to the environment, packaging will automatically change to an online address.

This variable can also be configured globally in Main.js.

Vue.prototype. $URL = Process.env.BASE_API;

The Vue CLI Scaffolding Project uses Webpack to configure different interface addresses or different variable values for the production and release environments.

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.