How to Switch Between Vue Production and Development Environments and Use of Filters

  • 2021-11-10 08:36:55
  • OfStack

Directory 1. Switching between production environment and development environment
Method 1: By configuring the. env file
Method 2
Step 2 Filter
3. Use of moment Time Library

1. Switch between production environment and development environment

Development environment: The development environment is to configure proxyTable under/config/index. js
Production environment: After packaging the project, the agent will fail, so it is necessary to reconfigure 1 under reproduction environment

Method 1: By configuring the. env file

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

Method 2

Step 1: Switch through cross-env by creating js files for different environments


 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 pass in package. json

Installation instructions: npm install cross-env -D

package. Configuration in 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 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 environment in business code


// Read process.env Constant object BUILD_ENV
const envType = process.env.BUILD_ENV;

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

// Create 1 A axios Instances 
const service = axios.create({
  baseURL: urlObj.BASE_URL + vipUrl
});

Step 2 Filter

1. Global Filters
Definition:


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

})

Use:


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

2. Local filter

3. Summary: Filter usage scenario: Used to process background data into the data format finally displayed by the user

Examples: 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 time, mm minutes, SS seconds");

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

At present, we are struggling to learn about the development environment and production environment, summarizing every day, improving every day, and entering the leading position in IT industry as soon as possible.


Related articles: