首页 > 解决方案 > Vue路由器在哈希模式下错误地处理url

问题描述

我的网站托管在 IIS 上,可以作为machinename/test/.
当我尝试以machinename/test/路由方式打开站点machinename/test/#/时,所有资产都按预期加载。
但是,如果我在machinename/test路由变为时打开machinename/test#/,并且资产路径中断。

我该如何解决?我想当去machinename/test的路径正在成为machinename/test/#/

标签: vue.jsvue-routervue-cli-3

解决方案


这有点hacky,但这应该可行:

VueRouter实例化之前,添加:

if (!window.location.pathname.endsWith('test/')) {
  window.location.replace(
    `${window.location.href}`.replace(
      window.location.pathname, 
      `${window.location.pathname}`.replace(
        '/test', 
        '/test/')
      )
  )
}


推荐阅读