vue.js - 如何部署 Vue.JS MPA(多页面应用程序)

标签 vue.js vue-component vue-router vuejs-routing

我使用 vue.js 和 vue.config.js 方法创建了一个多页应用程序我开发它没有问题我创建了一个生产版本我可以访问主页但是我无法访问其他页面,例如关于我们、投资组合等。
可能是什么问题??以及如何解决?是关于 .htacess 的吗?
这是我的 vue.config.js 代码

module.exports = {
    pages: {
      index: {
        entry: './src/pages/Home/main.js',
        template: 'public/index.html',
        title: 'Welcome to Appclust',
        chunks: ['chunk-vendors', 'chunk-common', 'index']
      },
      about:{
        entry: './src/pages/About/main.js',
        template: 'public/index.html',
        title: 'About us',
        chunks: ['chunk-vendors', 'chunk-common', 'about']
      },
      portfolio:{
        entry: './src/pages/Portfolio/main.js',
        template: 'public/index.html',
        title: 'Portfolio',
        chunks: ['chunk-vendors', 'chunk-common', 'portfolio']
      },
      testimonials:{
        entry: './src/pages/Testimonials/main.js',
        template: 'public/index.html',
        title: 'Testimonials',
        chunks: ['chunk-vendors', 'chunk-common', 'testimonials ']
      },
      careers:{
        entry: './src/pages/Careers/main.js',
        template: 'public/index.html',
        title: 'Careers',
        chunks: ['chunk-vendors', 'chunk-common', 'careers']
      },
      contact:{
        entry: './src/pages/Contact/main.js',
        template: 'public/index.html',
        title: 'Contact',
        chunks: ['chunk-vendors', 'chunk-common', 'contact']
      }
    }
  }
这是我的文件结构
enter image description here
生产文件
enter image description here

最佳答案

  • 启用 mode:history用于 vue-router 中没有 hashbang 的漂亮 URL
  • 由于您的项目从一开始就是 SPA,因此在新访问/刷新路由后,爬网程序/服务器无法识别您的路由(因为 /about 缺少预期的 index.html。)
  • 是的,您可以在 .htaccess 中设置重定向规则对于缺失的路线。
  • RewriteEngine On
    # If an existing asset or directory is requested go to it as it is
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^ - [L]
    
    # otherwise use history router
    RewriteRule ^ /index.html
    
  • 可选:如果您希望为每个路由生成预渲染的静态 HTML 文件,prerender-spa-plugin是您项目构建的一个很好的补充。如果您想从头开始构建 MPA,请转到 NUXT。
  • 关于vue.js - 如何部署 Vue.JS MPA(多页面应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62690348/

    相关文章:

    javascript - Vue.js - v-for "property or method is not defined"

    javascript - Vuejs : use imported plugin mixin localy

    gulp - 如何将 vue.js 与 gulp 一起使用?

    javascript - 如何在 VueJS 中使用 Date-FNS?

    firebase - Firestore 用户管理最佳实践?

    vue.js - 是否可以在 vue 2 中使用没有组件的 vue-router?

    javascript - 为什么组件 props 未定义(vue 路由器)

    javascript - VueJS 中的菜单折叠

    laravel - 带有 vue 2 组件的 vue 路由器不适用于 laravel 5.8

    vue.js - Vue router 保留 iframe dom 元素(只是隐藏,不要破坏)