当前位置: 首页 > 工具软件 > for-editor > 使用案例 >

vue js代码编辑插件(vue-prism-editor)

诸葛嘉熙
2023-12-01

示例:https://prism-editor.netlify.com/
插件地址:https://github.com/koca/vue-prism-editor

安装

npm install vue-prism-editor

or

yarn add vue-prism-editor

使用姿势

<template>
  <prism-editor :code="code" language="js"></prism-editor>
</template>

<script>
import PrismEditor from 'vue-prism-editor'
export default {
  components: {
    PrismEditor
  }
}
</script>

或者

import VuePrismEditor from "vue-prism-editor";
import "vue-prism-editor/dist/VuePrismEditor.css"; // import the styles
Vue.component("prism-editor", VuePrismEditor);

或者

<!-- vue-prism-editor JavaScript -->
<script src="https://unpkg.com/vue-prism-editor"></script>

<!-- vue-prism-editor CSS -->
<link rel="stylesheet" href="https://unpkg.com/vue-prism-editor/dist/VuePrismEditor.css">

<!-- use -->
<script>
Vue.component('vue-prism-editor', VuePrismEditor)
new Vue({
    el: '#app'
})
</script>

Prismjs

这个包并不会安装Prismjs,如果想使用Prismjs,需要额外自己安装。

// yarn add prismjs
import "prismjs";
import "prismjs/themes/prism.css";

或者

<link rel="stylesheet" href="https://unpkg.com/prismjs/themes/prism.css" />
<script src="https://unpkg.com/prismjs"></script>

Props

NameTypeDefaultOptionsDescription
v-modelstring--for the code prop below
codestring""-the code
languageString"js"vue,html,md,ts + Prismjs Languageslanguage of the code
lineNumbersBooleanfalse-Whether to show line numbers or not
readonlyBooleanfalse-Indicates if the editor is read only or not.
emitEventsBooleanfalse-Indicates if the editor should emit events.
autoStyleLineNumbersBooleantrue-Allow the line number to be styled by this component.

Events

NameParametersDescription
change(code)Fires when the code is changed.

The events below won’t be fired unless you set the emitEvents prop to true.

NameParametersDescription
keydown(event)This event is emitted when a keydown event happens in editor
keyup(event)This event is emitted when a keyup event happens in editor
editor-click(event)This event is emitted when clicking anywhere in the contenteditable editor

一个完整实例

<template>
<prism-editor :code="mycode" :lineNumbers="true"
               @change="codeChanged" language="python"></prism-editor>
</template>
<script>  
  import Vue from 'vue';
  import PrismEditor from 'vue-prism-editor';
  import "vue-prism-editor/dist/VuePrismEditor.css";
  //引入prismjs相关的包
  import "prismjs";
  import "prismjs/components/prism-python";
  import "prismjs/components/prism-python.min";
  //自己喜欢的主题
  import "prismjs/themes/prism-tomorrow.css";

  Vue.component("prism-editor", PrismEditor);     
   export default {
    data() {
      return {
        mycode: 'import json'       
      }
    },
    methods: {
      codeChanged(_code){
        window.console.log(_code);
      }
    }
  }
</script>                         
 类似资料: