当前位置:首页 > VUE

vue实现文档浏览

2026-01-17 04:17:54VUE

Vue 实现文档浏览的方法

使用 vue-markdown-loader 解析 Markdown 文件

安装依赖:

npm install vue-markdown-loader markdown-it --save-dev

配置 webpack:

// webpack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.md$/,
        loader: 'vue-markdown-loader',
        options: {
          preset: 'default',
          breaks: true,
          typographer: true
        }
      }
    ]
  }
}

组件中使用:

<template>
  <div v-html="compiledMarkdown"></div>
</template>

<script>
import markdownContent from './document.md'

export default {
  computed: {
    compiledMarkdown() {
      return markdownContent
    }
  }
}
</script>

使用第三方库 vue-markdown

安装:

npm install vue-markdown

基本用法:

<template>
  <vue-markdown :source="markdownText"></vue-markdown>
</template>

<script>
import VueMarkdown from 'vue-markdown'

export default {
  components: {
    VueMarkdown
  },
  data() {
    return {
      markdownText: '# Hello World\n\nThis is a markdown document.'
    }
  }
}
</script>

动态加载文档内容

通过 API 获取文档:

<template>
  <vue-markdown v-if="content" :source="content"></vue-markdown>
</template>

<script>
import VueMarkdown from 'vue-markdown'

export default {
  components: { VueMarkdown },
  data() {
    return {
      content: null
    }
  },
  async mounted() {
    const response = await fetch('/api/document')
    this.content = await response.text()
  }
}
</script>

实现文档导航功能

创建文档导航组件:

<template>
  <div class="document-viewer">
    <div class="sidebar">
      <ul>
        <li v-for="item in toc" :key="item.id">
          <a :href="`#${item.id}`">{{ item.text }}</a>
        </li>
      </ul>
    </div>
    <div class="content">
      <vue-markdown :source="content" @rendered="updateToc"></vue-markdown>
    </div>
  </div>
</template>

<script>
import VueMarkdown from 'vue-markdown'

export default {
  components: { VueMarkdown },
  data() {
    return {
      content: '',
      toc: []
    }
  },
  methods: {
    updateToc(renderer) {
      this.toc = renderer.getToc()
    }
  }
}
</script>

<style>
.document-viewer {
  display: flex;
}
.sidebar {
  width: 250px;
}
.content {
  flex: 1;
}
</style>

添加代码高亮支持

安装 highlight.js:

npm install highlight.js

配置代码高亮:

// main.js
import hljs from 'highlight.js'
import 'highlight.js/styles/github.css'

Vue.directive('highlight', function(el) {
  const blocks = el.querySelectorAll('pre code')
  blocks.forEach(block => {
    hljs.highlightBlock(block)
  })
})

在组件中使用:

<template>
  <div v-highlight>
    <vue-markdown :source="content"></vue-markdown>
  </div>
</template>

实现文档搜索功能

添加搜索功能:

<template>
  <div>
    <input v-model="searchQuery" placeholder="Search...">
    <vue-markdown :source="filteredContent"></vue-markdown>
  </div>
</template>

<script>
import VueMarkdown from 'vue-markdown'

export default {
  components: { VueMarkdown },
  data() {
    return {
      content: '',
      searchQuery: ''
    }
  },
  computed: {
    filteredContent() {
      if (!this.searchQuery) return this.content
      // 简单实现搜索高亮
      const regex = new RegExp(this.searchQuery, 'gi')
      return this.content.replace(regex, match => `==${match}==`)
    }
  }
}
</script>

实现文档版本切换

文档版本切换功能:

<template>
  <div>
    <select v-model="selectedVersion" @change="loadDocument">
      <option v-for="version in versions" :value="version" :key="version">
        {{ version }}
      </option>
    </select>
    <vue-markdown :source="content"></vue-markdown>
  </div>
</template>

<script>
import VueMarkdown from 'vue-markdown'

export default {
  components: { VueMarkdown },
  data() {
    return {
      versions: ['1.0', '2.0', '3.0'],
      selectedVersion: '1.0',
      content: ''
    }
  },
  methods: {
    async loadDocument() {
      const response = await fetch(`/docs/v${this.selectedVersion}.md`)
      this.content = await response.text()
    }
  },
  created() {
    this.loadDocument()
  }
}
</script>

这些方法提供了在 Vue 中实现文档浏览的基本功能,可以根据实际需求进行组合和扩展。

vue实现文档浏览

标签: 文档vue
分享给朋友:

相关文章

vue实现聊天

vue实现聊天

Vue实现聊天功能 基础项目结构搭建 使用Vue CLI或Vite创建项目,安装必要依赖如socket.io-client或axios(根据通信方式选择)。核心组件通常包括: ChatWindow.…

vue实现横向导航

vue实现横向导航

vue实现横向导航的方法 使用Flex布局实现横向导航 在Vue中可以通过Flex布局快速实现横向导航。创建一个组件,使用display: flex和flex-direction: row属性使导航项…

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed:…

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue实现用户信息删除

vue实现用户信息删除

Vue 实现用户信息删除功能 在 Vue 中实现用户信息删除功能通常涉及前端界面交互和后端 API 调用。以下是实现步骤: 前端界面设计 创建删除按钮或操作项,通常使用 @click 事件触发删除…

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用…