【发布时间】:2017-12-13 04:37:36
【问题描述】:
我正在尝试弄清楚如何从组件内检测 textarea 上值的变化。
对于输入,我们可以简单地使用
<input
:value="value"
@input="update($event.target.value)"
>
但是在 textarea 上这不起作用。
我正在使用的是 CKEditor 组件,当父组件(附加到此子组件)的模型值更新时,它应该更新所见即所得的内容。
我的Editor 组件目前看起来像这样:
<template>
<div class="editor" :class="groupCss">
<textarea :id="id" v-model="input"></textarea>
</div>
</template>
<script>
export default {
props: {
value: {
type: String,
default: ''
},
id: {
type: String,
required: false,
default: 'editor'
}
},
data() {
return {
input: this.$slots.default ? this.$slots.default[0].text : '',
config: {
...
}
}
},
watch: {
input(value) {
this.update(value);
}
},
methods: {
update(value) {
CKEDITOR.instances[this.id].setData(value);
},
fire(value) {
this.$emit('input', value);
}
},
mounted () {
CKEDITOR.replace(this.id, this.config);
CKEDITOR.instances[this.id].setData(this.input);
this.fire(this.input);
CKEDITOR.instances[this.id].on('change', () => {
this.fire(CKEDITOR.instances[this.id].getData());
});
},
destroyed () {
if (CKEDITOR.instances[this.id]) {
CKEDITOR.instances[this.id].destroy()
}
}
}
</script>
我将它包含在父组件中
<html-editor
v-model="fields.body"
id="body"
></html-editor>
然而,每当父组件的模型值发生变化时——它不会触发观察者——实际上不会更新编辑器的窗口。
我只需要在父组件的模型fields.body更新时调用update()方法。
关于我如何处理它的任何指针?
【问题讨论】:
-
vue textarea 值只使用
v-modelvuejs.org/v2/guide/forms.html#Multiline-text
标签: vue.js vuejs2 vue-component