当前位置:首页 > VUE

vue实现代码插件

2026-02-20 18:28:47VUE

Vue 实现代码插件的方法

使用 highlight.js 实现语法高亮

安装 highlight.jsvue 的封装库:

npm install highlight.js @highlightjs/vue-plugin

在 Vue 项目中注册插件:

import { createApp } from 'vue'
import App from './App.vue'
import hljs from 'highlight.js'
import 'highlight.js/styles/github.css' // 选择主题样式
import { HljsVuePlugin } from '@highlightjs/vue-plugin'

const app = createApp(App)
app.use(HljsVuePlugin)
app.mount('#app')

使用组件:

<template>
  <highlightjs language="javascript" :code="code" />
</template>

<script>
export default {
  data() {
    return {
      code: 'const message = "Hello, Vue!"'
    }
  }
}
</script>

自定义代码编辑器组件

创建一个可编辑的代码块组件:

vue实现代码插件

<template>
  <div class="code-editor">
    <pre><code :class="`language-${language}`">{{ code }}</code></pre>
    <textarea v-model="code" @input="updateCode"></textarea>
  </div>
</template>

<script>
export default {
  props: {
    language: {
      type: String,
      default: 'javascript'
    },
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      code: this.value
    }
  },
  methods: {
    updateCode() {
      this.$emit('input', this.code)
      this.highlight()
    },
    highlight() {
      // 使用 highlight.js 或其他库处理高亮
    }
  }
}
</script>

<style>
.code-editor {
  position: relative;
}
.code-editor textarea {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0.7;
}
</style>

使用 Monaco Editor 集成

安装 Monaco Editor:

npm install monaco-editor

创建 Vue 组件封装:

<template>
  <div ref="container" class="monaco-editor"></div>
</template>

<script>
import * as monaco from 'monaco-editor'

export default {
  props: {
    value: String,
    language: {
      type: String,
      default: 'javascript'
    },
    theme: {
      type: String,
      default: 'vs-dark'
    }
  },
  mounted() {
    this.editor = monaco.editor.create(this.$refs.container, {
      value: this.value,
      language: this.language,
      theme: this.theme,
      automaticLayout: true
    })

    this.editor.onDidChangeModelContent(() => {
      this.$emit('input', this.editor.getValue())
    })
  },
  beforeUnmount() {
    this.editor.dispose()
  }
}
</script>

<style>
.monaco-editor {
  height: 400px;
  width: 100%;
}
</style>

使用 Prism.js 实现轻量级高亮

安装 Prism.js:

vue实现代码插件

npm install prismjs

创建指令:

import Prism from 'prismjs'
import 'prismjs/themes/prism.css'

export default {
  mounted(el, binding) {
    const code = el.textContent
    const language = binding.value || 'javascript'
    el.innerHTML = Prism.highlight(code, Prism.languages[language], language)
  }
}

注册指令:

import { createApp } from 'vue'
import App from './App.vue'
import prismDirective from './directives/prism'

const app = createApp(App)
app.directive('prism', prismDirective)
app.mount('#app')

使用指令:

<template>
  <pre v-prism="'javascript'"><code>{{ code }}</code></pre>
</template>

标签: 插件代码
分享给朋友:

相关文章

jquery插件

jquery插件

jQuery 插件开发指南 jQuery 插件是一种扩展 jQuery 功能的模块化方式,允许开发者封装可重用的代码。以下是开发和使用 jQuery 插件的基本方法。 插件基本结构 jQuery 插…

vue滚动插件实现

vue滚动插件实现

vue滚动插件实现方法 使用vue-virtual-scroller 安装依赖包: npm install vue-virtual-scroller 注册组件: import VueVirtual…

vue插件实现联动

vue插件实现联动

Vue 插件实现联动的方法 使用全局事件总线 在 Vue 中可以通过创建一个全局事件总线来实现组件间的联动。首先创建一个新的 Vue 实例作为事件总线。 // event-bus.js import…

vue实现网站代码

vue实现网站代码

Vue 实现网站代码的基本步骤 安装 Vue.js 通过 npm 或 CDN 引入 Vue.js。使用 npm 安装:npm install vue。或者直接在 HTML 文件中引入 Vue CDN:…

vue实现动画插件

vue实现动画插件

Vue 动画插件实现方案 Vue 提供了多种实现动画的方式,可以通过内置的 <transition> 组件或第三方插件来实现复杂的动画效果。 使用 Vue 内置 <transiti…

vue实现代码插件

vue实现代码插件

Vue 实现代码插件的方法 使用第三方库(如 Prism.js 或 Highlight.js) 安装 Prism.js 或 Highlight.js 库,这些库专门用于代码高亮显示。在 Vue 项目中…