Written on the front
Note development environment or test environment of local proxy agents, when deployed to the official, the line must be replaced by IP address, otherwise, the data can not get oh
Proxy Configuration
easy mock analog interface address, you first explained how to configure and use, and only that part of the proxy configuration, no other explanation
New Project (skip le ...)
ip address is configured to automatically switch
In the first of a new project
vue.config.js (cli profile)
.env.development (developed and tested configuration interface address)
.env.production (production environment configuration interface address)
On top of that configuration file reference vue-cli documents, links this piece of content in the past
To put a project directory
.env.production (the contents below into them)
Interface address configuration test and the ## local development
VUE_APP_URL = "your development or test interface address."
.env.development (the contents below into them)
## Configuring the official address of the interface
VUE_APP_URL = "your official address of the interface."
vue.config.js (proxy configuration reference documents proxy agent )
The following test interface side ( https://www.easy-mock.com/mock/5ce2a7854c85c12abefbae0b/api )
The .env.development (the contents below into them) get rid of, and replaced with the address of the top
Interface address configuration test and the ## local development
VUE_APP_URL = "https://www.easy-mock.com/mock/5ce2a7854c85c12abefbae0b/api"
Vue.config.js configured under properties of devServer
const port = 8589; // dev port
// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
devServer: {
port,
open: true,
overlay: {
warnings: false,
errors: true
},
// 配置代理 (以接口 https://www.easy-mock.com/mock/5ce2a7854c85c12abefbae0b/api 说明)
proxy: {
"/api": {
target: process.env.VUE_APP_URL,
changeOrigin: true, // 是否改变域名
ws: true
// pathRewrite: {
// // 路径重写
// "/api": "" // 这个意思就是以api开头的,定向到哪里, 如果你的后边还有路径的话, 会自动拼接上
// }
}
}
// 下边这个, 如果你是本地自己mock 的话用after这个属性,线上环境一定要干掉
// after: require("./mock/mock-server.js")
}
};
Start the test
In main.js print address just to get on top of
import Vue from "vue";
import App from "./App.vue";
Vue.config.productionTip = false;
new Vue({
render: h => h(App)
}).$mount("#app");
console.log(process.env.VUE_APP_URL);
Print results
Test request (I main.js in the configuration)
import Vue from "vue";
import App from "./App.vue";
import axios from "axios";
Vue.config.productionTip = false;
// axios.get("")
axios.baseURL = process.env.VUE_APP_URL;
console.log(process.env.VUE_APP_URL);
axios.get("/api/new_article").then(res => {
console.log(res);
});
new Vue({
render: h => h(App)
}).$mount("#app");
Look at the network (network), the interface has been to the local agent
Look at the top of the comments pathRewrite use property
Modify .env.development file
Interface address configuration test and the ## local development
VUE_APP_URL = "https://www.easy-mock.com/mock/"
const port = 8589; // dev port
// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
devServer: {
port,
open: true,
overlay: {
warnings: false,
errors: true
},
// 配置代理 (以接口 https://www.easy-mock.com/mock/5ce2a7854c85c12abefbae0b/api 说明)
proxy: {
"/api": {
// 以 “/api” 开头的 代理到 下边的 target 属性 的值 中
target: process.env.VUE_APP_URL,
changeOrigin: true, // 是否改变域名
ws: true,
pathRewrite: {
// 路径重写
"/api": "5ce2a7854c85c12abefbae0b/api" // 这个意思就是以api开头的,定向到哪里, 如果你的后边还有路径的话, 会自动拼接上
}
}
}
// 下边这个, 如果你是本地自己mock 的话用after这个属性,线上环境一定要干掉
// after: require("./mock/mock-server.js")
}
};
Another benefit of this configuration is that as long as you change the interface address, the address of your proxy, you automatically change back, not reproduction and manual test environment changed back and forth,
Remember to change the complete configuration file to restart the project, to take effect
Put your own profiles
const path = require("path");
function resolve(dir) {
return path.join(__dirname, dir);
}
// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
const port = 9527; // dev port
// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
/**
* You will need to set publicPath if you plan to deploy your site under a sub path,
* for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
* then publicPath should be set to "/bar/".
* In most cases please use '/' !!!
* Detail: https://cli.vuejs.org/config/#publicpath
*/
publicPath: "/",
outputDir: "dist",
assetsDir: "static",
lintOnSave: process.env.NODE_ENV === "development",
productionSourceMap: false,
devServer: {
port,
open: true,
overlay: {
warnings: false,
errors: true
},
// 配置代理 (以接口 https://www.easy-mock.com/mock/5ce2a7854c85c12abefbae0b/api 说明)
proxy: {
"/api": {
// 以 “/api” 开头的 代理到 下边的 target 属性 的值 中
target: process.env.VUE_APP_URL,
changeOrigin: true, // 是否改变域名
ws: true,
pathRewrite: {
// 路径重写
"/api": "5ce2a7854c85c12abefbae0b/api" // 这个意思就是以api开头的,定向到哪里, 如果你的后边还有路径的话, 会自动拼接上
}
}
}
// 下边这个, 如果你是本地自己mock 的话用after这个属性,线上环境一定要干掉
// after: require("./mock/mock-server.js")
},
configureWebpack: {
// provide the app's title in webpack's name field, so that
// it can be accessed in index.html to inject the correct title.
resolve: {
// 配置别名
alias: {
"@": resolve("src")
}
}
},
// webpack配置覆盖
chainWebpack(config) {
config.plugins.delete("preload"); // TODO: need test
config.plugins.delete("prefetch"); // TODO: need test
// set svg-sprite-loader
config.module
.rule("svg")
.exclude.add(resolve("src/icons"))
.end();
config.module
.rule("icons")
.test(/\.svg$/)
.include.add(resolve("src/icons"))
.end()
.use("svg-sprite-loader")
.loader("svg-sprite-loader")
.options({
symbolId: "icon-[name]"
})
.end();
// set preserveWhitespace
config.module
.rule("vue")
.use("vue-loader")
.loader("vue-loader")
.tap(options => {
options.compilerOptions.preserveWhitespace = true;
return options;
})
.end();
config
// https://webpack.js.org/configuration/devtool/#development
.when(process.env.NODE_ENV === "development", config =>
config.devtool("cheap-source-map")
);
config.when(process.env.NODE_ENV !== "development", config => {
config
.plugin("ScriptExtHtmlWebpackPlugin")
.after("html")
.use("script-ext-html-webpack-plugin", [
{
// `runtime` must same as runtimeChunk name. default is `runtime`
inline: /runtime\..*\.js$/
}
])
.end();
config.optimization.splitChunks({
chunks: "all",
cacheGroups: {
libs: {
name: "chunk-libs",
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: "initial" // only package third parties that are initially dependent
},
elementUI: {
name: "chunk-elementUI", // split elementUI into a single package
priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
},
commons: {
name: "chunk-commons",
test: resolve("src/components"), // can customize your rules
minChunks: 3, // minimum common number
priority: 5,
reuseExistingChunk: true
}
}
});
config.optimization.runtimeChunk("single");
});
}
};