前端插件库之vue3使用vue-codemirror插件

vue3插件vue-codemirror使用步骤和实例

vue-codemirror

基于 CodeMirror ,适用于 Vue 的 Web 代码编辑器。

使用

1.命令行安装

npm install vue-codemirror --save
// cnpm install vue-codemirror --save

如果运行官网例子时, 报错:

@codemirror/lang-javascript
@codemirror/theme-one-dark

可以在终端中安装对应文件, 解决问题

npm i  @codemirror/lang-javascript
npm i  @codemirror/theme-one-dark

2.在需要的组件中配置

<templat
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
封装 CodeMirror 可以让我们在 Vue 中更方便地使用它,以下是一个简单的封装示例: 1. 安装 CodeMirror ```bash npm install codemirror ``` 2. 创建 Codemirror.vue 组件 ```html <template> <div> <textarea ref="textarea"></textarea> </div> </template> <script> import CodeMirror from 'codemirror' export default { name: 'Codemirror', props: { value: { type: String, default: '' }, options: { type: Object, default: () => ({}) } }, mounted() { this.cm = CodeMirror.fromTextArea(this.$refs.textarea, this.options) this.cm.on('change', this.handleChange) this.cm.setValue(this.value) }, beforeUnmount() { this.cm.off('change', this.handleChange) this.cm.toTextArea() }, methods: { handleChange() { this.$emit('input', this.cm.getValue()) } } } </script> ``` 3. 在需要使用的组件中引入 Codemirror 组件 ```html <template> <div> <Codemirror v-model="code" :options="options" /> </div> </template> <script> import Codemirror from './Codemirror.vue' import 'codemirror/lib/codemirror.css' import 'codemirror/theme/material.css' import 'codemirror/mode/javascript/javascript' export default { name: 'App', components: { Codemirror }, data() { return { code: `function sayHello() {\n console.log('Hello, world!')\n}`, options: { mode: 'javascript', theme: 'material', lineNumbers: true } } } } </script> ``` 以上示例中,我们封装了一个名为 Codemirror 的组件,它接受两个 props:value 和 options。value 表示 CodeMirror 的值,options 表示 CodeMirror 的选项。在组件中,我们使用了 CodeMirror.fromTextArea() 方法创建 CodeMirror 实例,并将其挂载到组件的 textarea 上。当值发生变化时,我们使用 v-model 语法糖和 $emit() 方法将值传递给父组件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值