vue history须要nginx或者其余方式配置一下才可正确访问,不然路由跳转以后刷新一下便会404 具体缘由vue-router官网有说明,在此很少说css
最近遇到的问题是上了一个小的项目,须要放在更深层次的目录下,上到测试环境 作了官网提到的nginx配置发现不行,以后查阅了一些博客资料,发现都没有一个特别好的方案。最终,个人解决方案以下html
nginx配置以下vue
server { listen 443 ; server_name m; root html/mobile; location / { index index.html index.htm; try_files $uri $uri/ /auth/index.html; } }
vue router 配置webpack
// 路由配置 const RouterConfig = { base: "/auth/", mode: 'history', routes: routers };
该项目是用的vue-cli2 因此只须要修改config 里面的index.js的build部分nginx
webpack 配置修改web
build: { // Template for index.html index: path.resolve(__dirname, '../dist/auth/index.html'), // Paths assetsRoot: path.resolve(__dirname, '../dist'), assetsSubDirectory: 'auth/static', assetsPublicPath: '/', /** * Source Maps */ productionSourceMap: false, devtool: '#source-map', productionGzip: false, productionGzipExtensions: ['js', 'css'], bundleAnalyzerReport: process.env.npm_config_report }
最终项目地址为 m.xxx.com/auth/vue-router