vue package.json设置全局变量

首先安装 cross-env

npm i cross-env -S

package.json

 "scripts": {
    "buildProd": "cross-env BASE_URL=http://xxxx node build/build.js",
    "buildTest": "cross-env BASE_URL=http://xxxxxxx node build/build.js",
    "startTest": "cross-env BASE_URL=http://xxxxxxx webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "build": "node build/build.js"
  },

 build/webpack.dev.conf.js和build/webpack.prod.conf.js做相应的修改
切记在dev.env.js和prod.env.js里面不要加BASE_URL哦,否则会被覆盖

webpack.dev.conf.js
new webpack.DefinePlugin({
    'process.env': require('../config/dev.env'),
    'process.env.BASE_URL': JSON.stringify(process.env.BASE_URL)
}),

webpack.prod.conf.js
new webpack.DefinePlugin({
    'process.env': env,
    'process.env.BUILD_ENV': JSON.stringify(process.env.BASE_URL) 
})

 运行package.json里面相应的命令,项目中打印 console.log(process.env.BASE_URL)

就可以取到对应的接口了

你可能感兴趣的:(vue)