首页 Vue 🍺

前言

Vue3 的锚点添加与 Vue2 有一些小差别。在这里记录一下

路由

在 createRouter 中添加下面代码,使用锚点,注意路由模式需为 createWebHistory

const router = createRouter({
  scrollBehavior(to) {
    if (to.hash) {
      return {
        el: to.hash,
        behavior: 'smooth',
        top: 45,
      }
    }
  },
  history: createWebHistory(),
  routes,
})

锚点

然后在项目中使用id或其他方法添加锚点。

<el-divider v-show="showShop" id="shop"></el-divider>

锚点跳转

location.href = "#shop";





文章评论

目录