How to remove hashbang #!
from url?
I found option to disable hashbang in vue router documentation ( http://vuejs.github.io/vue-router/en/options.html )
For Vue 3, change this :
const router = createRouter({
history: createWebHashHistory(),
routes,
});
To this :
const router = createRouter({
history: createWebHistory(),
routes,
});
Source : https://next.router.vuejs.org/guide/essentials/history-mode.html#hash-mode
const router = new VueRouter({
mode: 'history',
routes: [...]
})
And if you are using AWS amplify, check this article on how to configure server: Vue router’s history mode and AWS Amplify
The default mode for vue-router is hash mode - it uses the URL hash to simulate a full URL so that the page won't be reloaded when the URL changes.
To get rid of the hash, we can use the router's history mode, which leverages the history.pushState
API to achieve URL navigation without a page reload:
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
}
]
Reference
Hash is a default vue-router mode setting, it is set because with hash, application doesn't need to connect server to serve the url. To change it you should configure your server and set the mode to HTML5 History API mode.
For server configuration this is the link to help you set up Apache, Nginx and Node.js servers:
https://router.vuejs.org/guide/essentials/history-mode.html
Then you should make sure, that vue router mode is set like following:
vue-router version 2.x
const router = new VueRouter({
mode: 'history',
routes: [...]
})
To be clear, these are all vue-router modes you can choose: "hash" | "history" | "abstract".
You'd actually just want to set mode
to 'history'
.
const router = new VueRouter({
mode: 'history'
})
Make sure your server is configured to handle these links, though. https://router.vuejs.org/guide/essentials/history-mode.html