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

慈云数据 8个月前 (03-13) 技术支持 87 0

1、history模式

vue3切换路由模式——Hash 、histoary
(图片来源网络,侵删)

使用CreateWebHistory

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模式

vue3切换路由模式——Hash 、histoary
(图片来源网络,侵删)

使用createWebHashHistory

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

微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon