【问题标题】:Vue Routes does not work properly in productionVue Routes 在生产环境中无法正常工作
【发布时间】:2018-07-17 22:40:13
【问题描述】:

我是 vuejs 的新手。对于我的 Vuejs 应用程序,在运行“npm run build”之后,我无法在 Web 托管服务器中访问 '/foo/apple' 之类的 URL。它显示错误 404 我正在使用 HTML5 历史模式 (https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations),并且我在 dev-server.js 中实现了如下所示的“connect-history-api-fallback”:

const express = require('express')
const app = express()
app.use(require('connect-history-api-fallback')())

我的路由器/index.js

export default new Router({
  mode: 'history',
  {
      path: '/Product',
      name: 'Product',
      component: Product,
  },
  {
      path: '/Product/kraftbag',
      name: 'Kraftbag',
      component: Kraftbag
  },
}); 

我的网站http://americandunnage.n55lwi.info/。 我找了很多关于这个问题的帖子,但我仍然可以找到解决方案。

【问题讨论】:

    标签: vue.js server routing vuejs2 vue-router


    【解决方案1】:

    你可能错过了 hashbang (#)

    尝试#/foo/apple 而不是/foo/apple

    vue路由器的默认设置是使用hashbang方法。这依赖于使用 index.html(或任何默认为 / url)页面,因为它之后的所有内容都不会作为 url 位置的一部分,而是传递到应用程序中。例如,在锚点 <a href="index.html#first-heading">1st heading</a> 中,这将转到页面的一部分,如果您已经在该页面上,则不会被重定向。

    【讨论】:

    • 对不起,我没有解释好。我已经更新了我的代码,我正在使用 HTML5 历史模式。
    • 可以去掉历史模式吗?如果您想使用它,请确保您了解如何设置您的服务器以使其正常工作,即。 它成为需要处理的服务器问题,因此您需要提供有关服务器的更多信息。
    • 请注意router.vuejs.org/guide/essentials/history-mode.html 中的第 4 段,它说明了问题以及可能的服务器配置
    • 哦,我想我只是放弃了历史模式。现在效果很好。
    猜你喜欢
    • 2021-07-08
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2018-02-09
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    相关资源
    最近更新 更多