首页 > 解决方案 > 重定向所有包含#井号的链接

问题描述

将 Vue 路由器模式从哈希更改为历史记录后,旧链接不会将用户重定向到新 URL。

有些仍然使用旧链接。

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/#/',
      name: 'Home',
      component: Home
    },
    {
      path: '/',
      name: 'Home',
      component: Home
    },
  ]
})

我需要将所有现有的 URL 链接重定向到没有哈希的 URL。

标签: vue.jsredirectrouterhistory

解决方案


beforeEach您可以在挂钩中替换哈希:

router.beforeEach((to, from, next) => {
  if (to.fullPath.substr(0,2) === "/#") {
    const path = to.fullPath.substr(2);
    next(path);
    return;
  }
  next();
});

推荐阅读