鍍金池/ 問答/HTML/ vue-router history模式下,get請(qǐng)求沒有發(fā)送到后臺(tái)

vue-router history模式下,get請(qǐng)求沒有發(fā)送到后臺(tái)

為了避免丑陋的URL,使用vue-router的時(shí)候選擇了history模式,后端用的express,然后按照官網(wǎng)配置了以下
const history = require('connect-history-api-fallback');

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

但是當(dāng)前端發(fā)出的get請(qǐng)求,地址欄成為http://localhost:3000/?tab=technology 的時(shí)
候,請(qǐng)求并沒有發(fā)出去,返回的還是index.html文件,怎么樣才能讓請(qǐng)求能夠正確發(fā)送出去呢,看了connect-history-api-fallback的官方文檔,一團(tuán)漿糊還是不是很懂,有沒有大佬指點(diǎn)一下,或者有Git項(xiàng)目學(xué)習(xí)下也好啊,實(shí)在不知道要怎么搞了,按照官網(wǎng)的NGINX配置了之后完全是可以的,express到底要怎么搞呢

回答
編輯回答
我不懂

其實(shí),普通設(shè)置成
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...

2018年3月21日 03:51
編輯回答
墨小羽

如果是history模式的話 多配置一個(gè) 路徑
{
path: '*',
name: 'index',
component: index
},
完整的如下

export default new Router({

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

})

2018年8月31日 05:36
編輯回答
尐飯團(tuán)

新手的話建議直接看看vue-cli生成的項(xiàng)目的代碼

2017年10月30日 02:39