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

vue yaml代碼編輯器組件問題

 更新時間:2023年07月20日 14:19:01   作者:極值小白  
這篇文章主要介紹了vue yaml代碼編輯器組件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

一、前期準(zhǔn)備

此組件的功能主要依賴于codemirror,另外加入了js-yaml進(jìn)行語法檢查,方便在實時編輯時提示語法不正確的地方。

因此首先需要在項目中安裝codemirror與js-yaml:

二、組件源碼及說明

新建@/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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue使用qrcode生成二維碼的方法

    vue使用qrcode生成二維碼的方法

    這篇文章給大家介紹了vue使用qrcode生成二維碼的方法,在Vue中實現(xiàn)二維碼生成需要使用第三方庫來處理生成二維碼的邏輯,常用的庫有qrcode和vue-qrcode,所以接下來小編將給大家介紹vue?qrcode生成二維碼的方法示例,需要的朋友可以參考下
    2024-01-01
  • 一文帶你搞懂Vue?Loader是如何工作的

    一文帶你搞懂Vue?Loader是如何工作的

    Vue?Loader?作為一個?webpack?的?Loader,扮演著將?.vue?文件轉(zhuǎn)化為瀏覽器可執(zhí)行代碼的角色,下面就跟隨小編一起深入了解Vue?Loader?的技術(shù)細(xì)節(jié)與工作機(jī)制吧
    2024-12-12
  • vite配置別名并處理報錯:找不到模塊“xxx”或其相應(yīng)的類型聲明方法詳解

    vite配置別名并處理報錯:找不到模塊“xxx”或其相應(yīng)的類型聲明方法詳解

    我在學(xué)習(xí)vue3+vite+ts的時候,在配置別名這一步的時候遇到了一個問題,這篇文章主要給大家介紹了關(guān)于vite配置別名并處理報錯:找不到模塊“xxx”或其相應(yīng)的類型聲明的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • Vue實現(xiàn)電商網(wǎng)站商品放大鏡效果示例

    Vue實現(xiàn)電商網(wǎng)站商品放大鏡效果示例

    這篇文章主要為大家介紹了Vue實現(xiàn)電商網(wǎng)站商品放大鏡效果示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Vue2實現(xiàn)組件延遲加載的示例代碼

    Vue2實現(xiàn)組件延遲加載的示例代碼

    當(dāng)一個頁面需要加載較多個組件時,并且組件自身又比較復(fù)雜,如果一次性加載,可能等待時間較長,體驗不好,這個時候就需要延遲加載了,本文為大家介紹了Vue2實現(xiàn)組件延遲加載的示例代碼,需要的可以參考下
    2024-01-01
  • VScode中配置ESlint+Prettier詳細(xì)步驟(附圖文介紹)

    VScode中配置ESlint+Prettier詳細(xì)步驟(附圖文介紹)

    這篇文章主要介紹了VScode中配置ESlint+Prettier詳細(xì)步驟,文中通過代碼示例講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-12-12
  • vue源碼解析computed多次訪問會有死循環(huán)原理

    vue源碼解析computed多次訪問會有死循環(huán)原理

    這篇文章主要為大家介紹了vue源碼解析computed多次訪問會有死循環(huán)原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • vue中使用swiper輪播圖的正確姿勢(親測有效)

    vue中使用swiper輪播圖的正確姿勢(親測有效)

    最近在用Vue制作移動端項目,碰到了輪播圖的制作,接下來就讓小編一步一步帶大家動作制作吧,下面這篇文章主要給大家介紹了關(guān)于vue中使用swiper輪播圖的正確姿勢,需要的朋友可以參考下
    2022-06-06
  • vue3中單文件組件<script?setup>實例詳解

    vue3中單文件組件<script?setup>實例詳解

    <script?setup>是vue3中新引入的語法糖,目的是簡化使用Composition?API時冗長的模板代碼,下面這篇文章主要給大家介紹了關(guān)于vue3中單文件組件<script?setup>的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • vue3?中v-model語法糖示例詳解

    vue3?中v-model語法糖示例詳解

    vue3中的v-model相當(dāng)于vue2中的v-model和v-bind.sync 修飾符組合在一起的產(chǎn)物(擇優(yōu)整合)v-bind.sync 在 vue3 中被移除了可以在組件標(biāo)簽上使用多個 v-model 綁定屬性,使用參數(shù)區(qū)分,這篇文章主要介紹了vue3?中v-model語法糖,需要的朋友可以參考下
    2024-06-06

最新評論