前端vue中實(shí)現(xiàn)嵌入代碼編輯器的詳細(xì)代碼
前言
最近遇到需求,需要將代碼在前端進(jìn)行展示編輯,但是直接在文本展示會(huì)出現(xiàn)代碼不整齊情況,格式化就需要嵌入代碼編輯器。
老規(guī)矩廢話不多說,上代碼?。。。。。。。。。?!
一、使用 vue-prism-editor 插件實(shí)現(xiàn)
- 安裝
npm i prismjs vue-prism-editor -S // 或者 cnpm i prismjs vue-prism-editor //或者 yarn add prismjs vue-prism-editor
- 代碼實(shí)現(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 插件實(shí)現(xiàn)
- 安裝
npm i vue2-ace-editor -S // 或者 cnpm i vue2-ace-editor -S
- 代碼實(shí)現(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, //啟用基本自動(dòng)完成
enableSnippets: true, // 啟用代碼段
enableLiveAutocompletion: true, //啟用實(shí)時(shí)自動(dòng)完成
tabSize: 2, //標(biāo)簽大小
fontSize: 14, //設(shè)置字號(hào)
showPrintMargin: false //去除編輯器里的豎線
}
}
},
methods: {
// 編輯內(nèi)容改變時(shí)觸發(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可以實(shí)現(xiàn)代碼的提示功能
- 效果預(yù)覽
總結(jié)
到此這篇關(guān)于前端vue中實(shí)現(xiàn)嵌入代碼編輯器的文章就介紹到這了,更多相關(guān)前端vue嵌入代碼編輯器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue elementui簡易側(cè)拉欄的使用小結(jié)
這篇文章主要介紹了vue elementui簡易側(cè)拉欄的使用,增加了側(cè)拉欄,目的是可以選擇多條數(shù)據(jù)展示數(shù)據(jù),本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06
vuejs選中當(dāng)前樣式active的實(shí)例
今天小編就為大家分享一篇vuejs選中當(dāng)前樣式active的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vue-cli3訪問public文件夾靜態(tài)資源報(bào)錯(cuò)的解決方式
這篇文章主要介紹了vue-cli3訪問public文件夾靜態(tài)資源報(bào)錯(cuò)的解決方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
使用vue-infinite-scroll實(shí)現(xiàn)無限滾動(dòng)效果
vue-infinite-scroll插件可以無限滾動(dòng)實(shí)現(xiàn)加載更多,其作用是是當(dāng)滾動(dòng)條滾動(dòng)到距離底部的指定高度時(shí)觸發(fā)某個(gè)方法。這篇文章主要介紹了用vue-infinite-scroll實(shí)現(xiàn)無限滾動(dòng)效果,需要的朋友可以參考下2018-06-06
vue常用指令實(shí)現(xiàn)學(xué)生錄入系統(tǒng)的實(shí)戰(zhàn)
本文主要介紹了vue常用指令實(shí)現(xiàn)學(xué)生錄入系統(tǒng)的實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
vue3實(shí)現(xiàn)模擬地圖站點(diǎn)名稱按需顯示的功能(車輛模擬地圖)
最近在做車輛模擬地圖,在實(shí)現(xiàn)控制站點(diǎn)名稱按需顯示,下面通過本文給大家分享vue3實(shí)現(xiàn)模擬地圖站點(diǎn)名稱按需顯示的功能,感興趣的朋友跟隨小編一起看看吧2024-06-06
vue車牌號(hào)校驗(yàn)和銀行校驗(yàn)實(shí)戰(zhàn)
這篇文章主要介紹了vue車牌號(hào)校驗(yàn)和銀行校驗(yàn)實(shí)戰(zhàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01

