欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

前端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項目打包后打開頁面空白解決辦法

    vue項目打包后打開頁面空白解決辦法

    這篇文章主要介紹了vue項目打包后打開空白解決辦法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • Vue中過濾器定義以及使用方法實例

    Vue中過濾器定義以及使用方法實例

    過濾器的功能是對要顯示的數(shù)據(jù)進行格式化后再顯示,其并沒有改變原本的數(shù)據(jù),只是產(chǎn)生新的對應(yīng)的數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Vue中過濾器定義以及使用方法的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • Vue+ElementUI實現(xiàn)表單動態(tài)渲染、可視化配置的方法

    Vue+ElementUI實現(xiàn)表單動態(tài)渲染、可視化配置的方法

    這篇文章主要介紹了Vue+ElementUI實現(xiàn)表單動態(tài)渲染、可視化配置的方法,需要的朋友可以參考下
    2018-03-03
  • vue上傳圖片到oss的方法示例(圖片帶有刪除功能)

    vue上傳圖片到oss的方法示例(圖片帶有刪除功能)

    這篇文章主要介紹了vue上傳圖片到oss的方法示例(圖片帶有刪除功能),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • uni-app自定義導航欄按鈕|uniapp仿微信頂部導航條功能

    uni-app自定義導航欄按鈕|uniapp仿微信頂部導航條功能

    這篇文章主要介紹了uni-app自定義導航欄按鈕|uniapp仿微信頂部導航條,需要的朋友可以參考下
    2019-11-11
  • Vue生命周期詳解

    Vue生命周期詳解

    這篇文章詳細介紹了Vue的生命周期,文中通過代碼示例介紹的非常詳細。對大家的學習有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • Vue中如何使用base64編碼和解碼

    Vue中如何使用base64編碼和解碼

    這篇文章主要介紹了Vue中如何使用base64編碼和解碼問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 教你如何使用VUE組件創(chuàng)建SpreadJS自定義單元格

    教你如何使用VUE組件創(chuàng)建SpreadJS自定義單元格

    這篇文章主要介紹了使用VUE組件創(chuàng)建SpreadJS自定義單元格的方法,通常我們使用組件的方式是,在實例化Vue對象之前,通過Vue.component方法來注冊全局的組件,文中通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2022-01-01
  • vue的滾動條插件實現(xiàn)代碼

    vue的滾動條插件實現(xiàn)代碼

    這篇文章主要介紹了vue的滾動條插件實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09
  • vue-cli系列之vue-cli-service整體架構(gòu)淺析

    vue-cli系列之vue-cli-service整體架構(gòu)淺析

    這篇文章主要介紹了vue-cli系列之vue-cli-service整體架構(gòu)淺析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01

最新評論