我试图通过v-model
多次设置 inline Vue CKEDITOR 4组件的内容。我的问题是,在用户在编辑器中键入文本之前,这一直有效。之后,我传递给v-model
的所有内容都会被用户键入的内容覆盖。
为进一步澄清,我定义了使用的步骤和代码:
复制步骤:
预期行为:
实际行为:
代码:
<template>
<div>
<ckeditor
v-model="editorData"
type="inline"
/>
</div>
</template>
export default class EditorWrapper extends Vue {
// Vue component property that contains data sent to editor in steps 1. and 3.
@Prop({ type: String, default: '' })
externalData!: string;
// Vue data variable that is bound 2-way to the editor
editorData: string;
// watch for changes to the 'externalData' property
@Watch('externalData')
onPropertyChange(externalData) {
// explicitly set the editor text when not changed by the user (steps 1. and 3.)
this.editorData = externalData;
}
// watch for changes to the editor 'v-model' data (user and non-user changes)
@Watch('editorData')
onEditorDataChange(data) {
// this outputs the editor text - 'ccc', then 'aaabbb' - which is wrong
console.log(data);
}
}
答案 0 :(得分:1)
您只需要在双向数据绑定中使用计算机设置器:
https://jsfiddle.net/ellisdod/18ybd6o5/
@Prop({ type: String, default: '' })
value: string;
get _value() {
return this.value
}
set _value(val) {
this.$emit('input', val)
}
<ckeditor
v-model="_value"
type="inline"
/>
然后您可以在父母中更新:
<editor-wrapper v-model="updatableValue"/>