1.将mode改成history
const router = new VueRouter({
mode: 'history',
routes
})
2.兼容老的hash路由,比如项目开始是hash模式,有固定的分享链接在生产用了,这时候改成history,hash模式的链接访问会失效,得做处理,如下 在路由拦截里,将hash路由转化为history路由并做跳转
router.beforeEach((to, from, next) => {
if (to.hash) {
changeHashToHistory(to, next)
return
}
})
import queryString from 'query-string'
function changeHashToHistory(to, next) {
const [route, parmas] = to.hash.split('?')
let routePath = route.replace('#', '')
let query = parmas ? queryString.parse(parmas) : {}
next({ path: routePath, query })
}
这里的queryString是解析?后面的参数为对象,用qs这个库也行,我的项目用的是queryString