【问题标题】:Vue router handle urls incorrectly in hash modeVue路由器在哈希模式下错误地处理url
【发布时间】:2023-03-19 19:12:01
【问题描述】:

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

我该如何解决?我想去machinename/test时路径变成machinename/test/#/

【问题讨论】:

  • 我也面临同样的问题。

标签: vue.js vue-router vue-cli-3


【解决方案1】:

这有点 hacky,但这应该可以工作:

VueRouter 实例化之前,添加:

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

【讨论】:

    【解决方案2】:

    转到你的 routes.js 并更改它

    const router = new VueRouter({
      routes: []
    

    到这里

    const router = new VueRouter({
      mode: 'history',
    routes:[]
    })
    

    重建您的项目并重新上传

    Vue 参考:https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations

    【讨论】:

    • 感谢您的回复,但我正在寻找哈希模式下的解决方案。它存在吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2017-01-15
    • 2019-02-13
    • 1970-01-01
    • 2016-08-22
    • 2019-06-11
    • 1970-01-01
    相关资源
    最近更新 更多