【问题标题】:How to deploy a Vue.JS MPA (Multi Page Application)如何部署 Vue.JS MPA(多页面应用程序)
【发布时间】:2020-10-22 16:46:23
【问题描述】:

我使用 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']
      }
    }
  }

这是我的文件结构

生产文件

【问题讨论】:

  • 对于像多页这样的任务,我建议使用 nuxt 而不是 vue CLI
  • 你有没有想过这个问题?

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


【解决方案1】:
  1. 在没有 hashbang 的 vue-router 中启用 mode:history 以获得漂亮的 URL
  2. 由于您的项目从一开始就是 SPA,因此在新访问/刷新路由后,爬虫/服务器无法识别您的路由(因为 /about 缺少预期的 index.html。)
  3. 是的,您可以在.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
  1. 可选:如果您希望为每个路由生成预渲染的静态 HTML 文件,prerender-spa-plugin 是您项目构建的一个很好的补充。如果您想从头开始构建 MPA,请前往 NUXT。

【讨论】:

  • .htaccess 会完成这项工作吗?
  • 是的,它应该可以完成这项工作。以前使用带有共享主机的 AngularJS 解决了
  • 我应该复制粘贴同样的东西吗?我的意思是你给的htaccess??还是我应该做一些修改?我复制粘贴了同样的东西,但无论我点击哪个页面,它都会进入主页或索引页面。我删除了这一行“# else use history router RewriteRule ^ /index.html”然后当我尝试导航到其他页面时出现错误
  • @BeingMR.G 可能会有一些变化,你可以在这里参考更多关于 Vue 的历史模式:router.vuejs.org/guide/essentials/…
  • 动态关键词、描述、标题等可以使用vue-meta,同时兼容NUXT和Vue。如果解决了,请在答案上打勾:) 干杯
猜你喜欢
  • 2018-06-22
  • 2021-03-19
  • 2018-11-19
  • 2019-07-02
  • 1970-01-01
  • 2019-08-11
  • 2020-06-12
  • 1970-01-01
  • 2017-07-30
相关资源
最近更新 更多