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

vue yaml代碼編輯器組件問(wèn)題

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

一、前期準(zhǔn)備

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

因此首先需要在項(xiàng)目中安裝codemirror與js-yaml:

二、組件源碼及說(shuō)明

新建@/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提高語(yǔ)法檢查核心支持
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, // 顯示行號(hào)
      mode: 'text/x-yaml', // 語(yǔ)法model
      gutters: ['CodeMirror-lint-markers'],  // 語(yǔ)法檢查器
      theme: 'monokai', // 編輯器主題
      lint: true // 開啟語(yǔ)法檢查
    })
    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, // 顯示行號(hào)
      mode: 'text/x-yaml', // 語(yǔ)法model
      gutters: ['CodeMirror-lint-markers'],  // 語(yǔ)法檢查器
      theme: 'monokai', // 編輯器主題
      lint: true // 開啟語(yǔ)法檢查
    })

這里的配置只有幾個(gè)簡(jiǎn)單的參數(shù),個(gè)人認(rèn)為有這些功能已經(jīng)足夠了,更多的詳細(xì)參數(shù)配置可以移步官方文檔;

如果想讓編輯器支持其他語(yǔ)言,可以查看codemirror官方文檔的語(yǔ)法支持,這里我個(gè)人比較傾向下載codemirror源碼,可以看到對(duì)應(yīng)語(yǔ)法demo的源代碼,使用不同的語(yǔ)法在本組件中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>

四、效果截圖

使用效果:

在這里插入圖片描述

語(yǔ)法檢測(cè)效果:

在這里插入圖片描述

在這里插入圖片描述

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    vue3中單文件組件<script?setup>實(shí)例詳解

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

    vue3?中v-model語(yǔ)法糖示例詳解

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

最新評(píng)論