当前位置:首页 > VUE

vue代码框实现

2026-01-18 17:32:53VUE

Vue 代码框实现方法

在 Vue 中实现代码框功能通常需要结合语法高亮库或 UI 组件库。以下是几种常见实现方式:

使用第三方库 Prism.js

安装 Prism.js 及相关依赖:

npm install prismjs

在 Vue 组件中使用:

<template>
  <pre><code class="language-javascript">{{ code }}</code></pre>
</template>

<script>
import Prism from 'prismjs';
import 'prismjs/themes/prism.css';

export default {
  data() {
    return {
      code: 'const message = "Hello World";\nconsole.log(message);'
    }
  },
  mounted() {
    Prism.highlightAll();
  }
}
</script>

使用 highlight.js

安装 highlight.js:

vue代码框实现

npm install highlight.js

组件实现示例:

<template>
  <div>
    <pre v-highlightjs><code class="javascript">{{ code }}</code></pre>
  </div>
</template>

<script>
import hljs from 'highlight.js';
import 'highlight.js/styles/github.css';

export default {
  directives: {
    highlightjs(el) {
      const blocks = el.querySelectorAll('code');
      blocks.forEach(block => {
        hljs.highlightBlock(block);
      });
    }
  },
  data() {
    return {
      code: 'function test() {\n  return "test";\n}'
    }
  }
}
</script>

使用 UI 组件库

Element UI 实现方式:

vue代码框实现

<template>
  <el-card>
    <pre><code class="language-js">{{ code }}</code></pre>
  </el-card>
</template>

Vuetify 实现方式:

<template>
  <v-card>
    <v-card-text>
      <pre><code>{{ code }}</code></pre>
    </v-card-text>
  </v-card>
</template>

自定义样式实现

纯 CSS 实现基础代码框:

<template>
  <div class="code-container">
    <pre><code>{{ code }}</code></pre>
  </div>
</template>

<style>
.code-container {
  background: #f5f5f5;
  border-radius: 4px;
  padding: 16px;
  overflow-x: auto;
}
.code-container code {
  font-family: 'Courier New', monospace;
  color: #333;
}
</style>

动态代码高亮

对于需要动态更新的代码内容:

<template>
  <div>
    <textarea v-model="rawCode"></textarea>
    <pre><code class="language-js" ref="codeBlock">{{ rawCode }}</code></pre>
  </div>
</template>

<script>
import Prism from 'prismjs';

export default {
  data() {
    return {
      rawCode: ''
    }
  },
  watch: {
    rawCode() {
      this.$nextTick(() => {
        Prism.highlightElement(this.$refs.codeBlock);
      });
    }
  }
}
</script>

每种方法各有优劣,Prism.js 和 highlight.js 提供完善的语法高亮功能,而 UI 组件库则提供更丰富的样式和交互功能。根据项目需求选择合适方案即可。

标签: 代码vue
分享给朋友:

相关文章

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue实现拖放

vue实现拖放

Vue 实现拖放功能 Vue 中实现拖放功能通常可以通过 HTML5 的原生拖放 API 或第三方库(如 vuedraggable)来实现。以下是两种方法的详细说明。 使用 HTML5 原生拖放 A…

vue 路由 实现

vue 路由 实现

Vue 路由实现方法 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是实现 Vue 路由的详细步骤: 安装 Vue Router 通过 npm 或 ya…

vue slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 slo…

vue observer实现

vue observer实现

Vue Observer 实现原理 Vue 的响应式系统核心是通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)实现的 Observer 模式。以下是关键实现细…