vue-router history模式下,get请求没有发送到后台

新手上路,请多包涵

为了避免丑陋的URL,使用vue-router的时候选择了history模式,后端用的express,然后按照官网配置了以下
const history = require('connect-history-api-fallback');

app.use(history());
app.use(express.static(path.join(__dirname, '../client/dist')));

但是当前端发出的get请求,地址栏成为http://localhost:3000/?tab=technology 的时
候,请求并没有发出去,返回的还是index.html文件,怎么样才能让请求能够正确发送出去呢,看了connect-history-api-fallback的官方文档,一团浆糊还是不是很懂,有没有大佬指点一下,或者有Git项目学习下也好啊,实在不知道要怎么搞了,按照官网的NGINX配置了之后完全是可以的,express到底要怎么搞呢

阅读 3.9k
3 个回答
✓ 已被采纳新手上路,请多包涵

其实,普通设置成
const history = require('connect-history-api-fallback');
//这句代码需要在express.static上面
app.use(history());
app.use(express.static(path.join(__dirname, '../client/dist')));
这样就可以了,但是我这里不行,需要配置成下面这样:
app.use(history(

{
    htmlAcceptHeaders: ['text/html', 'application/xhtml+xml']
}

));
主要是要理解connect-history-api-fallback是要做什么以及是什么意思,参考一下这篇博文
https://blog.csdn.net/zzl1243...

如果是history模式的话 多配置一个 路径
{
path: '*',
name: 'index',
component: index
},
完整的如下

export default new Router({

mode: 'history',
routes: [
    {
        path: '*',
        name: 'index',
        component: index
    },
    {
        path: '/',
        name: 'index',
        component: index
    }
]

})

新手的话建议直接看看vue-cli生成的项目的代码

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题