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

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

相关文章

如何生成react代码

如何生成react代码

使用 Create React App 生成项目 安装 Node.js 后,通过命令行工具运行以下命令创建新项目: npx create-react-app my-app cd my-app npm…

vue抽奖代码实现

vue抽奖代码实现

Vue 抽奖代码实现 基础抽奖组件结构 使用 Vue 3 的 Composition API 实现抽奖功能,核心包括转盘动画、奖品列表和抽奖逻辑。以下为组件基础结构: <template>…

vue实现登录代码

vue实现登录代码

以下是一个基于 Vue.js 的登录功能实现示例,包含表单验证和异步请求处理: 基础组件结构 <template> <div class="login-container"&g…

vue代码实现管理

vue代码实现管理

Vue.js 管理后台实现方案 使用 Vue.js 构建管理后台系统通常涉及路由、状态管理、UI 组件库等核心模块。以下是关键实现方案: 基础框架搭建 安装 Vue CLI 创建项目: npm i…

vue简单代码实现

vue简单代码实现

以下是一个基于 Vue 3 的简单代码实现示例,包含基础功能演示: 基础模板结构 <!DOCTYPE html> <html lang="en"> <head>…

vue 实现点击复制 代码

vue 实现点击复制 代码

vue 实现点击复制功能 使用Vue实现点击复制功能可以通过原生JavaScript的document.execCommand或现代的Clipboard API。以下是几种常见实现方式: 使用doc…