How to remove hashbang from url?

后端 未结 11 609
生来不讨喜
生来不讨喜 2020-11-30 17:04

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 )

相关标签:
11条回答
  • 2020-11-30 17:48

    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

    0 讨论(0)
  • 2020-11-30 17:49
    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

    0 讨论(0)
  • 2020-11-30 17:49

    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

    0 讨论(0)
  • 2020-11-30 17:51

    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".

    0 讨论(0)
  • 2020-11-30 17:56

    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

    0 讨论(0)
提交回复
热议问题