vue3切换路由模式——Hash 、histoary

1、history模式

使用createWebHistory

bash 复制代码
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  }
]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})
export default router

2、hash模式

使用createWebHashHistory

bash 复制代码
import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
 
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  }
]
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes
})
export default router

综上所述:

history 对应 createWebHistory

hash 对应 createWebHashHistory

相关推荐
dualven_in_csdn11 小时前
一键起飞调用示例
android·java·javascript
英勇无比的消炎药12 小时前
新手必看玩转TinyRobot一定要避开这些坑
前端·vue.js
meilindehuzi_a12 小时前
通俗易懂掌握树与二叉树:定义、核心概念与JS实现遍历
javascript·ecmascript
胡志辉12 小时前
深入浅出理解浏览器事件循环:从一道输出题讲到 Chrome 源码
前端·javascript·面试
英勇无比的消炎药12 小时前
别再盲目混用AI组件库和传统组件库差距原来这么大
前端·vue.js
gz-郭小敏12 小时前
优化横向滚动展示大量数据的时候数据晃动问题
前端·javascript·html·css3
英勇无比的消炎药14 小时前
前端提效神器全新AI组件库TinyRobot改写日常开发模式
前端·vue.js
IMPYLH14 小时前
HTML 的 <a>元素
前端·javascript·html
ZengLiangYi14 小时前
本地向量数据库选型:vectra vs chroma vs hnswlib
javascript·数据库·后端