Vue中Router路由使用history模式发布后访问404

简介: 通过配置nginx和webpack,解决使用Vue中Router路由使用history模式发布后访问404错误

使用webpack打包发布,网页不能正常访问,先看下路由配置

export default new Router({
  mode: 'history',
  base: '/putian/',
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/ClusterManage',
      name: 'ClusterManage',
      component: ClusterManage
    },
    {
      path: '/MonitorWarn',
      name: 'MonitorWarn',
      component: MonitorWarn
    }
  ]
})

其中mode为history模式后,vue访问将不需要"#"。去掉mode一行,采用默认路由访问路径时如下:

http://localhost:8000/putian#/ClusterManage

使用history模式后,访问路径如下:

http://localhost:8000/putian/ClusterManage

开发环境可以正常访问,打包使用部署直接404,我这边使用的nginx,来部署,首先需要更改webpack的发布路径,我这边是在config/index.js,如下:

build: {
    // Template for index.html
    index: path.resolve(__dirname, '../dist/index.html'),
    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    /**
     * 发布路径
     */
    assetsPublicPath: '/putian/',
    /**
     * Source Maps
     */
    productionSourceMap: true,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',
    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],
    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  }

注意:index.js中assetsPublicPath: '/putian/',这个参数和Router路由中 base: '/putian/',保持一致。

有的同学可能环境架构跟我的不一致可以通过更改,webpack.cong.js代码如下:

module.exports = {
  context: path.resolve(__dirname, '../'),
  entry: {
    app: './src/main.js'
  },
  output: {
    path: config.build.assetsRoot,
    filename: '[name].js',
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  },
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src'),
    }
  },

其中output中publicPath参数更改这个就可以了。

到此还没完,访问还是有问题,访问时找不到ClusterManage相关文件

http://localhost:8000/putian/ClusterManage

会出现404错误,该如何处理,这是需要配置nginx,利用try_files特性,对404重新定位,配置如下

location /putian {

  alias C:\Users\lhq\IDEAProject\accurate-portrait-vue\dist;

  #root   html;

           index  index.html index.htm;

  try_files $uri $uri/ /putian/index.html;

}

 访问上面地址,这里的$uri就是putian/ClusterManage,通过转换地址为去访问

http://localhost:8000/putian/index.html

相关文章
|
3月前
|
JavaScript 前端开发 网络架构
vue 路由器history和hash工作模式
vue 路由器history和hash工作模式
|
3月前
|
JavaScript 数据可视化
vue-cli学习二:vue-cli3版本 创建vue项目后,Runtime-Compiler和Runtime-only两个模式详解;vue项目管理器;配置文件的配置在哪,以及如何配置
这篇文章详细介绍了Vue CLI 3版本创建项目时的Runtime-Compiler和Runtime-only两种模式的区别、Vue程序的运行过程、render函数的使用、eslint的关闭方法,以及Vue CLI 2和3版本配置文件的不同和脚手架3版本创建项目的配置文件配置方法。
223 3
vue-cli学习二:vue-cli3版本 创建vue项目后,Runtime-Compiler和Runtime-only两个模式详解;vue项目管理器;配置文件的配置在哪,以及如何配置
|
3月前
|
资源调度 JavaScript 前端开发
路由管理:Vue Router的使用和配置技巧
【10月更文挑战第21天】路由管理:Vue Router的使用和配置技巧
64 3
|
3月前
|
缓存 JavaScript 搜索推荐
Vue Router 导航钩子的使用场景
【10月更文挑战第13天】
27 1
|
3月前
|
JavaScript 搜索推荐 数据处理
Vue Router 导航钩子
【10月更文挑战第17天】需要注意的是,过度使用导航钩子可能会导致代码复杂度增加,因此需要合理规划和使用。此外,不同的钩子在不同的场景下具有不同的作用,需要深入理解其特点和用法,才能更好地发挥它们的价值。可以在实际项目中结合具体需求,充分利用这些导航钩子来打造更加智能、灵活的路由导航机制。
39 1
|
3月前
|
JavaScript API
vue 批量自动引入并注册组件或路由等等
【10月更文挑战第12天】 vue 批量自动引入并注册组件或路由等等
|
3月前
|
JavaScript 前端开发 API
vue3中常用插件的使用方法:按需引入自定义组件,自动导入依赖包,自动生成路由,自动生成模拟数据
vue3中常用插件的使用方法:按需引入自定义组件,自动导入依赖包,自动生成路由,自动生成模拟数据
1047 0
|
3月前
|
JavaScript 前端开发 UED
vue中vue-router路由懒加载(按需加载)的作用以及常见的实现方法
vue中vue-router路由懒加载(按需加载)的作用以及常见的实现方法
257 1
|
3月前
|
JavaScript 前端开发 UED
|
2月前
|
JavaScript UED
"Vue实战技巧大揭秘:一招解决路由跳转页面不回顶部难题,让你的单页面应用用户体验飙升!"
【10月更文挑战第23天】在Vue单页面应用中,点击路由跳转时,默认情况下页面不会自动滚动到顶部,这可能影响用户体验。本文通过一个新闻网站的案例,介绍了如何使用Vue-router的全局前置守卫和`scrollBehavior`方法,实现路由跳转时页面自动滚动到顶部的功能,提升用户浏览体验。
149 0