【问题标题】:Redirect all links containing # hash sign重定向所有包含#井号的链接
【发布时间】:2019-11-30 00:12:27
【问题描述】:

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

有些人仍在使用旧链接。

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

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

【问题讨论】:

    标签: vue.js redirect router history


    【解决方案1】:

    你可以替换beforeEach钩子中的hash:

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

    【讨论】:

      猜你喜欢
      • 2014-08-17
      • 2015-01-29
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      相关资源
      最近更新 更多