将tinymce封装成组件,通过v-model接收修改内容。
流程:通过watch监听props的value,调用tinymce的setContent(),这一步会将内容的光标重置,回到开始位置。
watch: {
value(newVal, oldVal) {
// console.log(this.hasChange, this.hasInit);
if (!this.hasChange && this.hasInit) {
this.$nextTick(() => {
window.tinymce && window.tinymce.get(this.tinymceId).setContent(newVal || '')
})
}
},
}
解决方法:
// 让光标定位到末尾
watch: {
value(newVal, oldVal) {
// console.log(this.hasChange, this.hasInit);
if (!this.hasChange && this.hasInit) {
this.$nextTick(() => {
window.tinymce && window.tinymce.get(this.tinymceId).setContent(newVal || '')
// 让光标定位到未尾
window.tinymce.activeEditor.selection.select(window.tinymce.activeEditor.getBody(), true)
window.tinymce.activeEditor.selection.collapse(false)
})
}
},