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

Docker uses the nsenter tool to enter the container

When using Docker containers, it is more convenie...

JavaScript ECharts Usage Explanation

I used ECharts when doing a project before. Today...

Why is there this in JS?

Table of contents 1. Demand 2. Solution 3. The fi...

The latest version of MySQL5.7.19 decompression version installation guide

MySQL version: MySQL Community Edition (GPL) ----...

HTML+CSS to achieve charging water drop fusion special effects code

Table of contents Preface: accomplish: Summarize:...

Vue easily realizes watermark effect

Preface: Use watermark effect in vue project, you...

Detailed explanation of how to use the Vue date time picker component

This article example shares the specific code of ...

mysql5.7.22 download process diagram

1. Go to the official website www.mysql.com and s...

Example of making XML online editor using js

Table of contents Preface The need for online XML...

SELinux Getting Started

Back in the Kernel 2.6 era, a new security system...

How to implement Docker volume mounting

The creation of the simplest hello world output i...