如何删除hashbang #!从url ?

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

有没有办法有干净的url?

例子:

不是:# !/家庭

但是:/家庭

谢谢!


当前回答

vue-router的默认模式是哈希模式——它使用URL哈希来模拟完整的URL,这样当URL更改时页面就不会被重新加载。 为了摆脱散列,我们可以使用路由器的历史模式,它利用了历史。pushState API实现URL导航,而不需要重新加载页面:

import {routes} from './routes'; //import the routes from routes.js    

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

new Vue({
    el: '#app',
    router,
    render: h => h(App)
});

routes.js

import ComponentName from './ComponentName';

export const routes = [
   {
      path:'/your-path'
      component:ComponentName
   }
]

参考

其他回答

vue-router的默认模式是哈希模式——它使用URL哈希来模拟完整的URL,这样当URL更改时页面就不会被重新加载。 为了摆脱散列,我们可以使用路由器的历史模式,它利用了历史。pushState API实现URL导航,而不需要重新加载页面:

import {routes} from './routes'; //import the routes from routes.js    

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

new Vue({
    el: '#app',
    router,
    render: h => h(App)
});

routes.js

import ComponentName from './ComponentName';

export const routes = [
   {
      path:'/your-path'
      component:ComponentName
   }
]

参考

散列是vue-router模式的默认设置,之所以设置它是因为使用散列,应用程序不需要连接服务器来提供url。要改变它,你应该配置你的服务器,并将模式设置为HTML5 History API模式。

对于服务器配置,这是帮助你设置Apache, Nginx和Node.js服务器的链接:

https://router.vuejs.org/guide/essentials/history-mode.html

然后你应该确保,vue路由器模式设置如下:

Vue-router version 2.x

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

需要明确的是,这些都是你可以选择的vue-router模式:"hash" | "history" | "abstract"。

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

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

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

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