How to switch between Vue production and development environments and use filters

How to switch between Vue production and development environments and use filters

1. Switch between production environment and development environment

Development environment: The development environment is to configure proxyTable under /config/index.js
Production environment: The proxy becomes invalid after the project is packaged, so it needs to be reconfigured in the production environment

The first method: by configuring the .env file

Reference: https://cli.vuejs.org/zh/guide/mode-and-env.html

Second method

Step 1: Create different environment js files and switch them through cross-env

 config
    dev.js    
    prod.js
  
dev.js
module.exports = {
  BASE_URL: "https://test.365msmk.com"
};

prod.js

module.exports = {
  BASE_URL: "https://www.365msmk.com"
};

Step 2: Install cross-env and configure the parameters to be passed in package.json

Installation instructions: npm install cross-env -D

Configuration in package.json

"scripts": {
    "serve": "cross-env BUILD_ENV=dev vue-cli-service serve",
    "build": "cross-env BUILD_ENV=prod vue-cli-service build"
  }

Step 3: Modify vue.config.js to add configuration for webpack

module.exports = {
 .....
  chainWebpack: config => {
    config.plugin("define").tap(args => {
      args[0]['process.env'].BUILD_ENV = JSON.stringify(process.env.BUILD_ENV);
      return args;
    });
  }
};

Switch the environment in the business code

//Read BUILD_ENV in the process.env constant object
const envType = process.env.BUILD_ENV;

const urlObj = require(`../config/${envType}.js`);

//Create an axios instance const service = axios.create({
  baseURL: urlObj.BASE_URL + vipUrl
});

2. Filters

1. Global filter definition:

Vue.filter('filter name',function(a,b,c) {
  //....
  
 return ...

})

use:

{{ num | filter name (v1, v2) }}

2. Local filter

3. Summary: Filter usage scenarios: used to process background data into the data format that users will eventually display

For example: gender, payment status, logistics status, timestamp. . . . . .

3. Use of moment time library

moment official website: momentjs.cn/docs/

Installation instructions: npm i moment

Format: moment(timestamp).format("YYYY year MM month DD day, HH hour mm minute SS second");

Format display: http://momentjs.cn/docs/#/displaying/

I am currently working hard to learn the development environment and production environment, summarizing every day, making progress every day, and becoming a leader in the IT industry as soon as possible.

This concludes this article on how to switch between Vue production and development environments and the use of filters. For more information on switching between Vue production and development environments, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue global environment switching problem

<<:  MySQL 8.0.17 decompression version installation and configuration method graphic tutorial

>>:  Steps to install RocketMQ instance on Linux

Recommend

Analysis of the reasons why Vue3 uses Proxy to implement data monitoring

Vue data two-way binding principle, but this meth...

IE6 distortion problem

question: <input type="hidden" name=...

Learning Vue instructions

Table of contents 1. v-text (v-instruction name =...

Some ways to solve the problem of Jenkins integrated docker plugin

Table of contents background Question 1 Error 2 E...

Add ico mirror code to html (favicon.ico is placed in the root directory)

Code: Copy code The code is as follows: <!DOCTY...

jQuery plugin to implement search history

A jQuery plugin every day - to make search histor...

Realization of real-time file synchronization between Linux servers

Usage scenarios For existing servers A and B, if ...

Detailed explanation of how to adjust Linux command history

The bash history command in Linux system helps to...

Tutorial on installing VMWare15.5 under Linux

To install VMWare under Linux, you need to downlo...

How to modify the time in centos virtual machine

The one above shows the system time, and the one ...

JS implements circular progress bar drag and slide

This article example shares the specific code of ...

Vue.js implements simple timer function

This article example shares the specific code of ...