前端vue中實現(xiàn)嵌入代碼編輯器的詳細代碼
更新時間:2024年07月30日 10:50:57 作者:張大炮er
隨著Web技術(shù)的不斷發(fā)展,前端開發(fā)也正日新月異,下面這篇文章主要給大家介紹了關(guān)于前端vue中實現(xiàn)嵌入代碼編輯器的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
前言
最近遇到需求,需要將代碼在前端進行展示編輯,但是直接在文本展示會出現(xiàn)代碼不整齊情況,格式化就需要嵌入代碼編輯器。
老規(guī)矩廢話不多說,上代碼?。。。。。。。。。。?/p>
一、使用 vue-prism-editor 插件實現(xiàn)
- 安裝
npm i prismjs vue-prism-editor -S // 或者 cnpm i prismjs vue-prism-editor //或者 yarn add prismjs vue-prism-editor
- 代碼實現(xiàn)
<template> <div> <prism-editor class="my-editor height-300" v-model="code" :highlight="highlighter" :line-numbers="lineNumbers"></prism-editor> <div @click="handleLog"> HelloWorld</div> </div> </template> <script> import { PrismEditor } from 'vue-prism-editor' import 'vue-prism-editor/dist/prismeditor.min.css' import { highlight, languages } from 'prismjs/components/prism-core' import 'prismjs/components/prism-clike' import 'prismjs/components/prism-javascript' import 'prismjs/themes/prism-tomorrow.css' export default { name: 'CodeEditor1', components: { PrismEditor }, data: () => ({ // 雙向綁定編輯器內(nèi)容值屬性 code: ``, // true為編輯模式, false只展示不可編輯 lineNumbers: true }), methods: { highlighter(code) { return highlight(code, languages.js) //returns html }, handleLog(){ console.log(this.code); } } } </script> <style lang="css" scoped> /* required class */ .my-editor { background: #2d2d2d; color: #ccc; font-family: Fira code, Fira Mono, Consolas, Menlo, Courier, monospace; font-size: 14px; line-height: 1.5; padding: 5px; } /* optional */ .prism-editor__textarea:focus { outline: none; } /* not required: */ .height-300 { height: 1000px; width: 1000px; } </style>
- 效果預(yù)覽
二、使用 vue2-ace-editor 插件實現(xiàn)
- 安裝
npm i vue2-ace-editor -S // 或者 cnpm i vue2-ace-editor -S
- 代碼實現(xiàn)
<template> <div class="codeEditBox"> <editor v-model="code" @init="editorInit" @input='codeChange' lang="javascript" :options="editorOptions" theme="chrome"></editor> </div> </template> <script> import Editor from 'vue2-ace-editor' export default { name: 'CodeEditor', components: { Editor }, data() { return { // 雙向綁定的編輯器內(nèi)容值屬性 code: 'console.log("Hello World");', editorOptions: { // 設(shè)置代碼編輯器的樣式 enableBasicAutocompletion: true, //啟用基本自動完成 enableSnippets: true, // 啟用代碼段 enableLiveAutocompletion: true, //啟用實時自動完成 tabSize: 2, //標簽大小 fontSize: 14, //設(shè)置字號 showPrintMargin: false //去除編輯器里的豎線 } } }, methods: { // 編輯內(nèi)容改變時觸發(fā) codeChange(val) { val //console.log(val) }, editorInit() { require('brace/theme/chrome') require('brace/ext/language_tools') //language extension prerequsite... require('brace/mode/yaml') require('brace/mode/json') require('brace/mode/less') require('brace/snippets/json') require('brace/mode/lua') require('brace/snippets/lua') require('brace/mode/javascript') require('brace/snippets/javascript') } } } </script> <style scoped> .codeEditBox { width: 100%; height: 200px; } </style>
- vue2-ace-editor相比于vue-prism-editor可以實現(xiàn)代碼的提示功能
- 效果預(yù)覽
總結(jié)
到此這篇關(guān)于前端vue中實現(xiàn)嵌入代碼編輯器的文章就介紹到這了,更多相關(guān)前端vue嵌入代碼編輯器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+ElementUI實現(xiàn)表單動態(tài)渲染、可視化配置的方法
這篇文章主要介紹了Vue+ElementUI實現(xiàn)表單動態(tài)渲染、可視化配置的方法,需要的朋友可以參考下2018-03-03uni-app自定義導航欄按鈕|uniapp仿微信頂部導航條功能
這篇文章主要介紹了uni-app自定義導航欄按鈕|uniapp仿微信頂部導航條,需要的朋友可以參考下2019-11-11教你如何使用VUE組件創(chuàng)建SpreadJS自定義單元格
這篇文章主要介紹了使用VUE組件創(chuàng)建SpreadJS自定義單元格的方法,通常我們使用組件的方式是,在實例化Vue對象之前,通過Vue.component方法來注冊全局的組件,文中通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2022-01-01vue-cli系列之vue-cli-service整體架構(gòu)淺析
這篇文章主要介紹了vue-cli系列之vue-cli-service整體架構(gòu)淺析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01