在使用 history: createWebHashHistory() 时,可以通过 $route.query.family 来获取 url 中的參數值。具體代碼如下:

import { createApp } from 'vue'
import { createWebHashHistory, createRouter } from 'vue-router'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/Home.vue')
    }
  ]
})

router.beforeEach((to, from, next) => {
  console.log(to.query.family) // 獲取參數family的值
  next()
})

createApp(App).use(router).mount('#app')

在上述代碼中,通過 router.beforeEach() 方法,可以在每次路由跳轉前獲取 $route.query.family 的值。

Vue Router 使用 createWebHashHistory() 获取 URL 参数 - family

原文地址: https://www.cveoy.top/t/topic/lCyM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录