Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js Vue路由在development server中正常工作,但在生产环境中不工作_Vue.js - Fatal编程技术网

Vue.js Vue路由在development server中正常工作,但在生产环境中不工作

Vue.js Vue路由在development server中正常工作,但在生产环境中不工作,vue.js,Vue.js,我正在用vuejs开发一个应用程序,并使用vue路由进行路由。 我创建了一个route.js作为: import Vue from 'vue' import VueRouter from 'vue-router' import Content from './components/Content.vue' Vue.use(VueRouter) const router = new VueRouter({ mode: 'history', routes: [ { pa

我正在用vuejs开发一个应用程序,并使用vue路由进行路由。 我创建了一个route.js作为:

import Vue from 'vue'
import VueRouter from 'vue-router'
import Content from './components/Content.vue'

Vue.use(VueRouter)

const router = new VueRouter({
mode: 'history',
routes: [
     {
        path: '/',
        component: Home,
        meta: {
            forVisitors: true,
            title: 'Univibe Plus',
        }
    },
    {
        path: '/content',
        component: Content,
        meta: {
            forVisitors: true,
            title: 'Content',
        }
    }
]
})

export default router

在运行
sudo npm run dev
时,路由工作正常,但当我使用
sudo npm run build
创建生产应用程序时,只有
/
页面工作,而
/content
页面不工作,并出现错误“无法获取页面/内容”。请帮帮我。

我找到了这个问题的解决方案。 从项目的根目录安装express(当然已经安装了,但实际上只是将其作为依赖项添加):

npm install --save express
将连接历史回退api作为依赖项安装:

npm install --save connect-history-api-fallback
在项目的根目录中创建server.js文件。(请参见下面的脚本。)

更新package.json启动脚本,以便现在将使用express而不是serve:

"start": "node server.js"
然后我将我的server.js更改为:

// server.js

const express = require('express')
const path = require('path')
const history = require('connect-history-api-fallback')
// ^ middleware to redirect all URLs to index.html

const app = express()
const staticFileMiddleware = express.static(path.join(__dirname))

app.use(staticFileMiddleware)
app.use(history())
app.use(staticFileMiddleware)

app.get('/', function (req, res) {
  res.render(path.join(__dirname + '/index.html'))
})

app.listen(5000, function () {
  console.log( 'Express serving on 5000!' )
})

对于此配置,您的routes.js文件中应该有
模式:“history”

您是否尝试过哈希模式而不是历史模式?但是,如果我需要在不使用哈希的情况下使用它,那么我应该怎么做?因此它可以在哈希模式下工作?如果是,则错误可能是由服务器配置引起的。您使用的是哪台服务器我正在使用节点。我已经实现了这个链接:,但现在我在浏览器上得到的错误是“manifest.02e135ecfd19b229f97c.js:1未捕获的语法错误:意外的令牌