Hash路由:

    1. import { createRouter, createWebHashHistory } from 'vue-router'
    2. import Home from '../views/Home.vue'
    3. const routes = [
    4. {
    5. path: '/',
    6. name: 'Home',
    7. component: Home
    8. },
    9. {
    10. path: '/about',
    11. name: 'About',
    12. // route level code-splitting
    13. // this generates a separate chunk (about.[hash].js) for this route
    14. // which is lazy-loaded when the route is visited.
    15. component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    16. }
    17. ]
    18. const router = createRouter({
    19. history: createWebHashHistory(),
    20. routes
    21. })
    22. export default router

    History路由:

    1. import { createRouter, createWebHistory } from 'vue-router'
    2. import Home from '../views/Home.vue'
    3. const routes = [
    4. {
    5. path: '/',
    6. name: 'Home',
    7. component: Home
    8. },
    9. {
    10. path: '/about',
    11. name: 'About',
    12. // route level code-splitting
    13. // this generates a separate chunk (about.[hash].js) for this route
    14. // which is lazy-loaded when the route is visited.
    15. component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    16. }
    17. ]
    18. const router = createRouter({
    19. history: createWebHistory(process.env.BASE_URL),
    20. routes
    21. })
    22. export default router

    区别:

    1. // History:
    2. import { createRouter, createWebHistory } from 'vue-router'
    3. // Hash:
    4. import { createRouter, createWebHashHistory } from 'vue-router'
    5. // History:
    6. const router = createRouter({
    7. history: createWebHistory(process.env.BASE_URL),
    8. routes
    9. })
    10. // Hash:
    11. const router = createRouter({
    12. history: createWebHashHistory(),
    13. routes
    14. })