当前位置:首页 > VUE

vue 实现目录

2026-01-11 21:42:17VUE

在 Vue 中实现目录功能

使用 vue-router 动态生成路由目录

安装 vue-router 并配置动态路由,通过遍历路由信息生成目录结构:

// router/index.js
const routes = [
  { path: '/', name: 'Home', component: Home },
  { path: '/about', name: 'About', component: About },
  { path: '/contact', name: 'Contact', component: Contact }
]

在组件中通过 $router.options.routes 获取路由信息:

<template>
  <div class="catalog">
    <ul>
      <li v-for="route in routes" :key="route.name">
        <router-link :to="route.path">{{ route.name }}</router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  computed: {
    routes() {
      return this.$router.options.routes
    }
  }
}
</script>

基于页面标题自动生成目录

通过解析页面中的标题标签(h1-h6)自动生成目录结构:

<template>
  <div class="auto-catalog">
    <div v-html="content" ref="content"></div>
    <ul class="toc">
      <li v-for="(item, index) in toc" :key="index" :style="{ paddingLeft: `${item.level * 10}px` }">
        <a :href="`#${item.id}`">{{ item.text }}</a>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: '<h1 id="title1">标题1</h1><h2 id="title2">标题2</h2><h3 id="title3">标题3</h3>',
      toc: []
    }
  },
  mounted() {
    this.generateTOC()
  },
  methods: {
    generateTOC() {
      const headings = this.$refs.content.querySelectorAll('h1, h2, h3, h4, h5, h6')
      this.toc = Array.from(headings).map(heading => ({
        id: heading.id,
        text: heading.textContent,
        level: parseInt(heading.tagName.substring(1))
      }))
    }
  }
}
</script>

使用第三方库实现高级目录功能

安装 vue-toc 等专门处理目录的库:

npm install vue-toc

在组件中使用:

<template>
  <div>
    <div v-html="content" ref="content"></div>
    <vue-toc :content="content" :options="tocOptions"></vue-toc>
  </div>
</template>

<script>
import VueToc from 'vue-toc'

export default {
  components: { VueToc },
  data() {
    return {
      content: '<h1>主标题</h1><h2>子标题</h2>',
      tocOptions: {
        headings: 'h1, h2, h3'
      }
    }
  }
}
</script>

实现滚动定位和高亮当前目录项

添加滚动监听和目录项高亮功能:

<template>
  <div class="scroll-toc">
    <div class="content" ref="content">
      <!-- 内容区域 -->
    </div>
    <ul class="toc-list">
      <li v-for="(item, index) in toc" 
          :key="index" 
          :class="{ active: activeIndex === index }"
          @click="scrollTo(item.id)">
        {{ item.text }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      toc: [],
      activeIndex: 0
    }
  },
  mounted() {
    this.generateTOC()
    window.addEventListener('scroll', this.onScroll)
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.onScroll)
  },
  methods: {
    generateTOC() {
      // 生成目录逻辑
    },
    scrollTo(id) {
      const element = document.getElementById(id)
      element.scrollIntoView({ behavior: 'smooth' })
    },
    onScroll() {
      // 计算当前可见的标题并更新activeIndex
    }
  }
}
</script>

响应式目录组件设计

创建可复用的目录组件:

vue 实现目录

<template>
  <div class="catalog-container">
    <slot name="content"></slot>
    <div class="catalog-sidebar" :class="{ fixed: isFixed }">
      <h3>目录</h3>
      <ul>
        <li v-for="(item, index) in tocItems" 
            :key="index"
            :class="['level-' + item.level, { active: currentItem === item.id }]">
          <a :href="`#${item.id}`">{{ item.text }}</a>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    selector: {
      type: String,
      default: 'h2, h3'
    }
  },
  data() {
    return {
      tocItems: [],
      currentItem: '',
      isFixed: false
    }
  },
  mounted() {
    this.initTOC()
    this.initScrollListener()
  },
  methods: {
    initTOC() {
      const elements = document.querySelectorAll(this.selector)
      this.tocItems = Array.from(elements).map(el => ({
        id: el.id || this.generateId(el.textContent),
        text: el.textContent,
        level: parseInt(el.tagName.substring(1))
      }))
    },
    generateId(text) {
      return text.toLowerCase().replace(/\s+/g, '-')
    },
    initScrollListener() {
      // 实现滚动监听逻辑
    }
  }
}
</script>

标签: 目录vue
分享给朋友:

相关文章

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…

vue实现drag

vue实现drag

Vue 实现拖拽功能的方法 在 Vue 中实现拖拽功能可以通过原生 HTML5 的拖拽 API 或第三方库如 vuedraggable 来完成。以下是两种常见方法的实现方式。 使用 HTML5 拖拽…

vue实现登出

vue实现登出

实现Vue登出功能 登出功能通常涉及清除用户会话、令牌或本地存储的数据,并重定向到登录页面。以下是几种常见的实现方式: 清除用户令牌和状态 在Vuex的store中定义一个logout mutati…

vue实现webapp

vue实现webapp

Vue 实现 WebApp 的基本步骤 环境准备 确保已安装 Node.js 和 npm/yarn。通过 Vue CLI 或 Vite 创建项目: npm install -g @vue/cli v…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安装…

vue实现登录拦截

vue实现登录拦截

实现登录拦截的方法 在Vue项目中,可以通过路由守卫(Navigation Guards)实现登录拦截,确保未登录用户无法访问受保护的路由。以下是具体实现方式: 使用全局前置守卫 在路由配置文件中(…