search
HomeWeb Front-endJS TutorialDetailed explanation of how Vue packages domain names based on parameter methods

This time I will bring you a detailed explanation of vue's method of packaging domain names based on parameter methods. What are the precautions for vue to package domain names based on parameter methods. The following is a practical case, let's take a look.

During project development, the front-end is very troubled when configuring the back-end api domain name. It often appears:

Local development environment: api-dev.demo.com

Test environment: api-test.demo.com

Online production environment: api.demo.com,

This time it is packaged in the Vue.js project, and I will teach you how to do it:

Use npm run build -- xxx , according to the passed parameter xxx to determine different environments and give different domain name configurations.

1. Modification of /config/dev.env.js in the project:

Added: HOST: '"dev"'

'use strict'
const merge = require('webpack-merge')
const prodEnv = require('./prod.env')
module.exports = merge(prodEnv, {
 NODE_ENV: '"development"',
 HOST: '"dev"'
})

2. Modification of /config/prod.env.js in the project:

Get the parameters passed in:

'use strict'
let HOST = process.argv.splice(2)[0] || 'prod';
console.log(HOST);
module.exports = {
 NODE_ENV: '"production"',
 HOST: '"'+HOST+'"'
}

3. Modification of ajax encapsulation in the project :

/**
** 设置API接口域名
**/
let apiUrl = '';
// 根据 process.env.HOST 的值判断当前是什么环境
// 命令:npm run build -- test ,process.env.HOST就设置为:'test'
let HOST = process.env.HOST;
HOST = HOST === 'prod' ? '' : '-' + HOST;
apiUrl = 'http://api'+HOST+'.demo.com';
axios.defaults.baseURL = apiUrl;

4. Finally type the command:

npm run build -- test

Note – Yes 2 horizontal bars, followed by parameters, so process.env.HOST will get the parameter 'test',

apiUrl = 'http://api-test.demo.com'

If prod is released and packaged online,npm run build -- prod

apiUrl = 'http://api.demo.com'

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

jQuery implements image upload with local preview function

vue axios request interception implementation idea ( Code attached)

Angular implementation of server-side rendering steps detailed explanation

The above is the detailed content of Detailed explanation of how Vue packages domain names based on parameter methods. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
主机的域名和主机的ip地址两者之间的关系是什么主机的域名和主机的ip地址两者之间的关系是什么Jan 14, 2021 pm 06:02 PM

主机的域名和主机的ip地址两者之间的关系是:一个IP地址对应多个域名。IP地址用数字化形式来对计算机网络中的主机进行网络标识,域名用字符化形式来对计算机网络中的主机进行网络标识。在Internet中,一个域名之内能够对应一个IP地址,但是一个IP地址可以被多个域名所对应。

请求的控件无效 NET HELPMSG 2191:2 个简单修复请求的控件无效 NET HELPMSG 2191:2 个简单修复Apr 15, 2023 am 09:13 AM

在TCP/IP协议套件中,域名系统是提供计算机名称到IP地址映射名称解析服务的协议之一。但是,有时它会出现故障,从而导致错误,例如请求的控制对此服务NETHELPMSG2191无效。DNS客户端和服务器协同工作,为计算机和用户提供计算机名称到IP地址映射名称解析服务。安装Windows后,客户端和服务器版本的操作系统默认启用客户端服务。一旦您在TCP/IP网络配置中指定了服务器的IP地址,DNS客户端就会查询服务器以发现域控制器并将计算机名称解析为IP地址。只有在服务

分享PyCharm项目打包的简易方法分享PyCharm项目打包的简易方法Dec 30, 2023 am 09:34 AM

简单易懂的PyCharm项目打包方法分享随着Python的流行,越来越多的开发者使用PyCharm作为Python开发的主要工具。PyCharm是功能强大的集成开发环境,它提供了许多方便的功能来帮助我们提高开发效率。其中一个重要的功能就是项目的打包。本文将介绍如何在PyCharm中简单易懂地打包项目,并提供具体的代码示例。为什么要打包项目?在Python开发

如何使用Python正则表达式进行代码打包和分发如何使用Python正则表达式进行代码打包和分发Jun 23, 2023 am 09:31 AM

随着Python编程语言的日益流行,越来越多的开发者开始使用Python编写代码。但是在实际使用中,我们常常需要将这些代码打包并分发给其他人使用。本文将介绍如何使用Python正则表达式进行代码打包和分发。一、Python代码打包在Python中,我们可以使用setuptools和distutils等工具来打包我们的代码。这些工具可以将Python文件、模块

怎么使用pkg将Node.js项目打包为可执行文件?怎么使用pkg将Node.js项目打包为可执行文件?Jul 26, 2022 pm 07:33 PM

如何用pkg打包nodejs可执行文件?下面本篇文章给大家介绍一下使用pkg将Node.js项目打包为可执行文件的方法,希望对大家有所帮助!

国内永久免费域名申请方法是什么国内永久免费域名申请方法是什么Jan 19, 2024 pm 04:01 PM

申请方法:1、通过工信部网站申请免费域名,等待审核通过后即可获得免费域名;2、通过DNSPod免费申请域名,用户可以在DNSPod上注册账号并申请免费的二级域名;3、选择一个可靠的免费域名服务提供商,通过对比和了解,选择一个可靠的提供商;4、注册账号,点击注册按钮,按照要求填写相关个人信息并创建账号;5、搜索并选择域名即可。

linux打包是什么意思linux打包是什么意思Feb 23, 2023 pm 06:30 PM

在linux中,打包指的是一个文件或目录的集合,而这个集合被存储在一个文件中;简单来说,打包是指将一大堆文件或目录变成一个总的文件。打包文件没有经过压缩,因此它占用的空间是其中所有文件和目录的总和。

Discuz域名修改操作指南Discuz域名修改操作指南Mar 09, 2024 pm 04:36 PM

Discuz域名修改操作指南在使用Discuz论坛系统的过程中,有时候我们需要修改论坛的域名。可能是因为需要更换域名,或者是修复一些域名解析的问题。本文将为大家详细介绍如何在Discuz论坛系统中进行域名修改操作,并给出一些具体的代码示例。1.备份数据在进行任何操作之前,我们都要先备份数据,以防止因操作失误导致数据丢失。在Discuz中,可以使用后台的数据备

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools