Vue 2 contentEditable with v-model [英] Vue 2 contentEditable with v-model

查看:30
本文介绍了Vue 2 contentEditable with v-model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个类似于 Medium 的文本编辑器.我正在使用内容可编辑的段落标签,并将每个项目存储在一个数组中,并使用 v-for 呈现每个项目.但是,我在使用 v-model 将文本与数组绑定时遇到了问题.似乎与 v-model 和 contenteditable 属性存在冲突.这是我的代码:

<button class="toolbar" v-on:click.prevent="stylize('bold')">粗体</button>

<div v-for="(value, index) in content"><p v-bind:id="'content-'+index" v-bind:ref="'content-'+index" v-model="content[index].value" v-on:keyup="emit_content($event)" v-on:keyup.delete="remove_content(index)" contenteditable></p>

在我的脚本中:

出口默认{数据() {返回 {内容:[{值:''}]}},方法: {风格化(风格){document.execCommand(style, false, null);},删除内容(索引){if(this.content.length > 1 && this.content[index].value.length == 0) {this.content.splice(index, 1);}}}}

我还没有在网上找到任何答案.

解决方案

我昨天想通了!解决了这个解决方案.我基本上只是通过更新任何可能的事件来手动跟踪我的 content 数组中的 innerHTML 并通过手动分配具有动态引用的相应元素重新渲染,例如content-0, content-1,... 效果很好: