做过前端的都知道,一般我们上线的时候大多都会用browerRouter路由,这玩意需要服务器结合,就是在打包的时候需要加个baseUrl的前缀 例如www.baidu.com/cwxt/ 我们要加个cwxt这样的子目录,不然当访问的www.xxx.com这样的地址的时候默认就是访问首页去了
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: '/stationweb/', //打包nginx的要配置的前缀
//publicPath: './',
outputDir: 'dist', //最终是要在服务器上这样访问http:xxx.com/stationweb/dist
assetsDir: './static', //相当于css js最终是要在http:xxx.com/stationweb/dist/static的目录下
lintOnSave: process.env.NODE_ENV === 'development',
productionSourceMap: false,
devServer: {
port: port,
open: true,
https: false,
hot: true,
overlay: {
warnings: false,
errors: true
},
proxy: {
[process.env.VUE_APP_BASE_API]: { // /cwxt
target: baseUrl,
changeOrigin: true, //必须要加上这个才可以跨域
ws: true,
secure: false,
pathRewrite: {
['^' + process.env.VUE_APP_BASE_API]: ''
}
},
//转发activity地址
[activityPrefixUrl]: { // /activity
target: devBaseUrl,
changeOrigin: true, //必须要加上这个才可以跨域
ws: true,
secure: false, // 当代理某些https服务报错时用
pathRewrite: {
['^' + activityPrefixUrl]: ''
}
},
//转发station-platform的url地址
[stationPlatPrefixUrl]: {
target: localStationPlatBaseUrl,
changeOrigin: true, //必须要加上这个才可以跨域
ws: true,
secure: false, // 当代理某些https服务报错时用
pathRewrite: {
['^' + stationPlatPrefixUrl]: ''
}
},
//油站数据看板
[stationCenterPrefixUrl]: {
target: localStationCenterBaseUrl,
changeOrigin: true, //必须要加上这个才可以跨域
ws: true,
secure: false, // 当代理某些https服务报错时用
pathRewrite: {
['^' + stationCenterPrefixUrl]: ''
}
},
//油站11前缀
[stationCenter11PrefixUrl]: {
target: localStationCenter11BaseUrl,
changeOrigin: true, //必须要加上这个才可以跨域
ws: true,
secure: false, // 当代理某些https服务报错时用
pathRewrite: {
['^' + stationCenter11PrefixUrl]: ''
}
},
}
},
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.
name: name,
resolve: {
alias: {
'@': resolve('src')
}
}
},
chainWebpack(config) {
// it can improve the speed of the first screen, it is recommended to turn on preload
config.plugin('preload').tap(() => [
{
rel: 'preload',
// to ignore runtime.js
// https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
include: 'initial'
}
])
// when there are many pages, it will cause too many meaningless requests
config.plugins.delete('prefetch')
// 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()
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
}
}
})
// https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
config.optimization.runtimeChunk('single')
}
)
}
}
最后记得要在本地改下baseUrl:
const config = require('../../vue.config.js')
const createRouter = () => new Router({
base: config.publicPath,//加入路由前缀
mode: 'history', // require service support hash
//mode: 'hash', // hash
scrollBehavior: () => ({ y: 0 }),
routes: constantRoutes
})
最后在nginx设置转发,示例代码如下:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /stationweb/ {
alias D:/software/nginx-1.18.0/nginx-1.18.0/html/dist/; //这个表示当用户访问www.xxx/stationweb/xxx这样的地址时候,直接访问该nginx服务器指向的该目录
index index.html index.htm;
try_files $uri $uri/ /stationweb/index.html; #// 需要指定 your_folder 解决刷新404问题
}
}