前端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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+ElementUI實(shí)現(xiàn)表單動(dòng)態(tài)渲染、可視化配置的方法
這篇文章主要介紹了Vue+ElementUI實(shí)現(xiàn)表單動(dòng)態(tài)渲染、可視化配置的方法,需要的朋友可以參考下2018-03-03
uni-app自定義導(dǎo)航欄按鈕|uniapp仿微信頂部導(dǎo)航條功能
這篇文章主要介紹了uni-app自定義導(dǎo)航欄按鈕|uniapp仿微信頂部導(dǎo)航條,需要的朋友可以參考下2019-11-11
教你如何使用VUE組件創(chuàng)建SpreadJS自定義單元格
這篇文章主要介紹了使用VUE組件創(chuàng)建SpreadJS自定義單元格的方法,通常我們使用組件的方式是,在實(shí)例化Vue對(duì)象之前,通過Vue.component方法來注冊(cè)全局的組件,文中通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-01-01
vue的滾動(dòng)條插件實(shí)現(xiàn)代碼
這篇文章主要介紹了vue的滾動(dòng)條插件實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
vue-cli系列之vue-cli-service整體架構(gòu)淺析
這篇文章主要介紹了vue-cli系列之vue-cli-service整體架構(gòu)淺析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01

