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

示例: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>                         
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值