当前位置:首页 > 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

基本用法:

vue实现文档浏览

<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:

vue实现文档浏览

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实现多选div

vue实现多选div

实现多选 DIV 的基本思路 在 Vue 中实现多选 DIV 功能,可以通过以下方法实现。主要思路是利用 v-model 绑定选中状态,结合 CSS 类名动态切换样式。 数据准备与模板结构 定义一个…

vue 实现简单登陆

vue 实现简单登陆

以下是一个使用 Vue 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-containe…

用vue实现滑动输入条

用vue实现滑动输入条

使用 Vue 实现滑动输入条 使用原生 HTML5 input range 实现 在 Vue 模板中直接使用 <input type="range">,通过 v-model 绑定数据:…

vue评分实现

vue评分实现

Vue 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from '…

vue实现erp

vue实现erp

Vue实现ERP系统的关键步骤 技术选型与架构设计 采用Vue 3 + TypeScript构建前端,搭配Pinia状态管理,Element Plus或Ant Design Vue作为UI框架。后端可…

vue代理实现

vue代理实现

Vue 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.conf…