如何删除hashbang #!从url ?

我在vue路由器文档(http://vuejs.github.io/vue-router/en/options.html)中找到了禁用hashbang的选项,但这个选项删除了#!然后输入#

有没有办法有干净的url?

例子:

不是:# !/家庭

但是:/家庭

谢谢!


当前回答

你应该添加模式历史到你的路由器如下所示

export default new Router({
  mode: 'history',
  routes: [
    {
     ...
    }
  ]
})

其他回答

只需将router.js文件中的createWebHashHistory替换为createWebHistory

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

如果您正在使用AWS amplify,请查看关于如何配置服务器的本文:Vue路由器的历史模式和AWS amplify

对于Vue 3,更改如下:

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

对此:

const router = createRouter({
    history: createWebHistory(),
    routes,
});

来源:https://next.router.vuejs.org/guide/essentials/history-mode.html#hash-mode

打开src->router->index.js文件

在这个文件的底部:

const router = new VueRouter({
  mode: "history",
  routes,
});

在Vue 3中,你需要使用createWebHistory作为历史选项。

import { createRouter, createWebHashHistory } from 'vue-router'

const router = createRouter({
  history: createWebHashHistory(),
  // ...
})

在Vue 2中,你需要将模式设置为“历史”。

const router = new VueRouter({
  mode: 'history',
  // ...
})

但是,请确保您的服务器配置为处理这些链接。 https://router.vuejs.org/guide/essentials/history-mode.html