日期: 2021/07/05 天气:多云
背景
将原有的hash模式的路由,改为history模式,后台是node,需要利用connect-history-api-fallback包来配置
官方用法
①先下载包
npm i connect-history-api-fallback --save
②在app.js的启动文件配置
const express = require('express');
const app = express();
③如果public下只部署了一个项目,且是根目录,为index.html,只需要采用默认设置,这个要在引入静态文件之前引用!
app.use(history());
如果public下只部署了一个项目,且是根目录,为xxx.html,需要改下配置
history({
rewrites: [
{ from: /\/xxx/, to: '/xxx.html'}
]
});
注意
如果一个public文件夹下部署了多个,比如admin/xxxx,member/xxx,配置为
history({
rewrites: [
{ from: /\/admin/, to: '/admin/index.html'},
{ from: /\/member/, to: '/member/index.html'},
]
});
这样会报错,也算是一个小坑吧,
需要加一行代码:
app.use(history({
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],//这句很重要,能解决谷歌安全性问题
rewrites: [
{ from: /\/admin/, to: '/admin/index.html'},
{ from: /\/member/, to: '/member/index.html'},
]
}));