Vue.js v-html contenteditable防止dom刷新以防止光标/插入符跳转

Vue.js

樱古一

2020-03-16

For reference I'm using Vue 2.0, Vuex, and Firebase.

I am building a contenteditable component using the v-html binding to render the innerHTML. The data is updated onKeyUp. Whenever the data is updated the DOM element refreshes with the "new" data, causing the caret / cursor to jump back to the beginning of the contenteditable div.

I have looked into Rangy and a few other stackoverflow solutions, but I feel the easiest solution would be to unbind the DOM element from the data refresh. I would like for the data to still update in firebase but not result in an element refresh.

Is there a way for me to still use v-html but prevent the DOM element from refreshing with the data? Or is there another way to render the HTML without auto binding?

Edit: 11/18/16

So I’ve continued to work on a fix for this. Here are my current ideas.

  1. Use a lifecycle hook and stop component re-render. I’ve looked through the Vue docs but can’t seem to find something to stop the cycle.
  2. Use something like React’s “componentShouldRender”. Again, it doesn’t look like Vue.js has a comparable method in the lifecycle.

If anybody knows of any methods to end the lifecycle, stop re-render, or a way to get React's "componentShouldRender" functionality out of vue, that should be enough fix this issue.

-

Update: 11/29/16

This update is a little late coming. I've logged a feature request with Vue on Github.

There are a few JSFiddles in the issue discussion which could provide a potential solution. However none of them I believe qualifies as a complete solution. The only promising one has recently yielded more issues.

All of these problems would be non-issues with the addition of a componentShouldRender lifecycle hook. I will continue to look for a complete solution

第1665篇《Vue.js v-html contenteditable防止dom刷新以防止光标/插入符跳转》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

2个回答
阿飞飞云 2020.03.16

有没有办法让我仍然使用v-html但阻止DOM元素刷新数据?

是。v-once指令正是这样做的。

https://vuejs.org/v2/api/#v-once

Davaid番长 2020.03.16

据我了解,问题在于事件传播或冒泡,从而使vue重新渲染div,从而导致十字体跳到其开头。

在您的小提琴示例中event.preventDeafault();setContent()方法添加行即可完成工作。

有关更多详细信息-vue文档中“内联处理程序”中的方法部分

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android