vue.js - 重定向所有包含#井号的链接
问题描述
将 Vue 路由器模式从哈希更改为历史记录后,旧链接不会将用户重定向到新 URL。
有些仍然使用旧链接。
const router = new Router({
mode: 'history',
routes: [
{
path: '/#/',
name: 'Home',
component: Home
},
{
path: '/',
name: 'Home',
component: Home
},
]
})
我需要将所有现有的 URL 链接重定向到没有哈希的 URL。
解决方案
beforeEach
您可以在挂钩中替换哈希:
router.beforeEach((to, from, next) => {
if (to.fullPath.substr(0,2) === "/#") {
const path = to.fullPath.substr(2);
next(path);
return;
}
next();
});
推荐阅读
- c# - 如何保持 WPF UserControl 加载并使用 HwndSource(异常问题:Visual Target 的根 Visual 不能有父级)
- javascript - 如何从 javascript 访问多个 div 元素(块级元素),以更改所有 elememts 的转换属性?
- java - 如何为推送通知 (FCM) 启用声音?
- regex - 正则表达式在€符号之前找到除了浮动之外的所有内容
- azure - 如果我的应用仅请求 User.Read 权限,为什么需要管理员授权?
- jmeter - 如何在 JSR223 采样器中使用 StringFromFile 函数?
- python-3.x - 用 x =df2.index .values, y = df2.coulmn.values 散点图
- java - Android 中的错误:android.view.InflateException:二进制 XML 文件第 49 行:膨胀类错误
- scala - 并行执行 Monix 任务
- azure-devops - 如何在多阶段 YAML 管道中添加部署前和部署后批准?