vue env file

create file

1
2
# touch env.[custom]
touch env.test

env.test
custom var must start with VUE_APP_

1
2
3
NODE_ENV=development
BASE_URL=/
VUE_APP_MODE=test

usage

1
2
3
4
// get envs
const { VUE_APP_MODE: MODE = null } = process.env || {};
// do something
if (MODE === 'test') require('@/mock').default;

do like angularjs

以下方案不好,但仍有参考价值,做保留
最好的方案请查看vue config files config

create two config files

1
2
3
▾ configs/
config.test.ts
config.ts

config.test.ts

1
export default { baseURL: '/' };

config.ts

1
2
3
4
5
6
7
8
9
10
const { VUE_APP_MODE: MODE = null } = process.env || {};
import configTest from '@/configs/config.test';

let config = { baseURL: 'http://domain.com/' };

if (MODE === 'test') {
config = { ...config, ...configTest };
}

export default config;

usage: get config file
set axios baseURL

1
2
3
4
5
6
7
8
9
10
11
12
import config from '@/configs/config';
import axios from 'axios';

const { baseURL } = config;

const instance = axios.create({
baseURL,
timeout: 3000
// headers: { 'X-Custom-Header': 'foobar' }
});

export default instance;

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×