vue yaml代碼編輯器組件問題
一、前期準(zhǔn)備
此組件的功能主要依賴于codemirror,另外加入了js-yaml進(jìn)行語法檢查,方便在實(shí)時編輯時提示語法不正確的地方。
因此首先需要在項(xiàng)目中安裝codemirror與js-yaml:
- codemirror:
npm install codemirror - js-yaml:
npm install js-yaml --save
二、組件源碼及說明
新建@/components/YamlEditor/index.vue文件:
<template>
<div class="yaml-editor">
<textarea ref="textarea" />
</div>
</template><script>
import CodeMirror from 'codemirror'
import 'codemirror/addon/lint/lint.css'
import 'codemirror/lib/codemirror.css'
import 'codemirror/theme/monokai.css'
import 'codemirror/mode/yaml/yaml'
import 'codemirror/addon/lint/lint'
import 'codemirror/addon/lint/yaml-lint'
window.jsyaml = require('js-yaml') // 引入js-yaml為codemirror提高語法檢查核心支持
export default {
name: 'YamlEditor',
// eslint-disable-next-line vue/require-prop-types
props: ['value'],
data() {
return {
yamlEditor: false
}
},
watch: {
value(value) {
const editorValue = this.yamlEditor.getValue()
if (value !== editorValue) {
this.yamlEditor.setValue(this.value)
}
}
},
mounted() {
this.yamlEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
lineNumbers: true, // 顯示行號
mode: 'text/x-yaml', // 語法model
gutters: ['CodeMirror-lint-markers'], // 語法檢查器
theme: 'monokai', // 編輯器主題
lint: true // 開啟語法檢查
})
this.yamlEditor.setValue(this.value)
this.yamlEditor.on('change', (cm) => {
this.$emit('changed', cm.getValue())
this.$emit('input', cm.getValue())
})
},
methods: {
getValue() {
return this.yamlEditor.getValue()
}
}
}
</script><style scoped>
.yaml-editor{
height: 100%;
position: relative;
}
.yaml-editor >>> .CodeMirror {
height: auto;
min-height: 300px;
}
.yaml-editor >>> .CodeMirror-scroll{
min-height: 300px;
}
.yaml-editor >>> .cm-s-rubyblue span.cm-string {
color: #F08047;
}
</style>codemirror的核心配置如下:
this.yamlEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
lineNumbers: true, // 顯示行號
mode: 'text/x-yaml', // 語法model
gutters: ['CodeMirror-lint-markers'], // 語法檢查器
theme: 'monokai', // 編輯器主題
lint: true // 開啟語法檢查
})這里的配置只有幾個簡單的參數(shù),個人認(rèn)為有這些功能已經(jīng)足夠了,更多的詳細(xì)參數(shù)配置可以移步官方文檔;
如果想讓編輯器支持其他語言,可以查看codemirror官方文檔的語法支持,這里我個人比較傾向下載codemirror源碼,可以看到對應(yīng)語法demo的源代碼,使用不同的語法在本組件中import相應(yīng)的依賴即可。
三、組件使用
<template>
<div>
<div class="editor-container">
<yaml-editor v-model="value" />
</div>
</div>
</template><script>
import YamlEditor from '@/components/YamlEditor/index.vue';
const yamlData = "- hosts: all\n become: yes\n become_method: sudo\n gather_facts: no\n\n tasks:\n - name: \"install {{ package_name }}\"\n package:\n name: \"{{ package_name }}\"\n state: \"{{ state | default('present') }}\"";
export default {
name: 'YamlEditorDemo',
components: { YamlEditor },
data() {
return {
value: yamlData,
};
},
};
</script><style scoped>
.editor-container{
position: relative;
height: 100%;
}
</style>四、效果截圖
使用效果:

語法檢測效果:


總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Element-ui DatePicker顯示周數(shù)的方法示例
這篇文章主要介紹了Element-ui DatePicker顯示周數(shù)的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
vue發(fā)送驗(yàn)證碼計(jì)時器防止刷新實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了vue發(fā)送驗(yàn)證碼計(jì)時器防止刷新實(shí)現(xiàn)詳解,<BR>有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
基于Vue3+WebSocket實(shí)現(xiàn)實(shí)時文件傳輸監(jiān)控系統(tǒng)
WebSocket是一種在客戶端和服務(wù)器之間進(jìn)行雙向通信的網(wǎng)絡(luò)協(xié)議,它通過建立持久性的、全雙工的連接,允許服務(wù)器主動向客戶端發(fā)送數(shù)據(jù),本文小編給大家介紹了基于Vue3+WebSocket實(shí)現(xiàn)實(shí)時文件傳輸監(jiān)控系統(tǒng),需要的朋友可以參考下2025-03-03
vue實(shí)現(xiàn)分環(huán)境打包步驟(給不同的環(huán)境配置相對應(yīng)的打包命令)
這篇文章主要介紹了vue實(shí)現(xiàn)分環(huán)境打包步驟(給不同的環(huán)境配置相對應(yīng)的打包命令),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
webpack4+express+mongodb+vue實(shí)現(xiàn)增刪改查的示例
這篇文章主要介紹了webpack4+express+mongodb+vue 實(shí)現(xiàn)增刪改查的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
vue 中directive功能的簡單實(shí)現(xiàn)
本篇介紹directive的簡單實(shí)現(xiàn),主要學(xué)習(xí)其實(shí)現(xiàn)的思路及代碼的設(shè)計(jì),需要的朋友參考下吧2018-01-01
vue實(shí)現(xiàn)虛擬滾動渲染成千上萬條數(shù)據(jù)
本文主要介紹了vue實(shí)現(xiàn)虛擬滾動渲染成千上萬條數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02

