【问题标题】:Vue+Webpack app deploy on heroku not working when page is refreshed刷新页面时,在heroku上部署的Vue + Webpack应用程序不起作用
【发布时间】:2017-12-12 10:46:06
【问题描述】:

我使用 vue-cli 和 webpack 模板生成了一个 vue 应用程序,并使用 this guide 将其部署到 heroku。我能够毫无问题地运行它,但是当我刷新页面或直接访问子路由时,它会失败。

server.js

var express = require('express')
var path = require('path')
var serveStatic = require('serve-static')
app = express()
app.use(serveStatic(__dirname))
var port = process.env.PORT || 5000
app.listen(port)
console.log('server started '+ port)

router.js

我的 vue 路由器配置

export default new Router({ mode: 'history', routes })

【问题讨论】:

  • 能否发布控制台日志,如果上面有任何错误?开发菜单可以通过 Chrome 上的ctrl + shift + j 激活。
  • @Mikael 这不是基于代码的问题,因此控制台上不会出现错误。

标签: heroku vuejs2 vue-router


【解决方案1】:

水疗中心通常只有一个index.html,应用程序中其他路线的导航由 javascript 本身处理。当您刷新或直接访问子路由时,这会导致失败。

由于您在 vuejs 路由器配置中使用mode: 'history' 配置了history 模式,因此您可以考虑使用connect-history-api-fallback,如here: Example server configurations 所述。

  1. npm install --save connect-history-api-fallback
  2. 将此中间件添加到您的快递中

    var history = require('connect-history-api-fallback');
    
    var express = require('express')
    var path = require('path')
    var serveStatic = require('serve-static')
    app = express()
    //add this middleware
    app.use(history());    
    app.use(serveStatic(__dirname))
    var port = process.env.PORT || 5000
    app.listen(port)
    console.log('server started '+ port) 
    

【讨论】:

  • 很棒的解决方案!!
  • 有没有办法在不更改后端代码(在我的例子中是 node.js)的情况下解决这个问题?
猜你喜欢
  • 1970-01-01
  • 2022-06-16
  • 2017-05-26
  • 2020-10-01
  • 2017-08-21
  • 2017-12-09
  • 2021-10-28
  • 2015-12-23
  • 1970-01-01
相关资源
最近更新 更多