vue.js - VueJS : Textarea input binding

标签 vue.js vuejs2 vue-component

我正在尝试找出如何从组件内检测文本区域值的变化。

对于输入,我们可以简单地使用

<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() 方法。

关于我如何处理它的任何指示?

最佳答案

这是一些需要破译的代码,但我要做的是将文本区域和 WYSIWYG HTML 窗口分解为两个不同的组件,然后让父级同步这些值,因此:

TextArea 组件:

<template id="editor">
  <textarea :value="value" @input="$emit('input', $event.target.value)" rows="10" cols="50"></textarea>
</template>

/**
 *  Editor TextArea
 */
Vue.component('editor', {
  template: '#editor',
  props: {
    value: {
      default: '',
      type: String
    }
  }
});

我在这里所做的就是在输入发生变化时将输入发回给父级,我将输入用作事件名称并将 value 用作 Prop ,因此我可以使用 v -model 在编辑器上。现在我只需要一个所见即所得的窗口来显示代码:

所见即所得窗口:

/**
 * WYSIWYG window
 */
Vue.component('wysiwyg', {
  template: `<div v-html="html"></div>`,
  props: {
    html: {
      default: '',
      type: String
    }
  }
}); 

那里没什么大不了的,它只是渲染作为 prop 传递的 HTML。

最后我只需要同步组件之间的值:

<div id="app">
  <wysiwyg :html="value"></wysiwyg>
  <editor v-model="value"></editor>
</div>

new Vue({
  el: '#app',
  data: {
    value: '<b>Hello World</b>'
  }
})

现在,当编辑器更改时,它会将事件发回父级,父级更新 value 并依次在所见即所得窗口中触发该更改。这是整个过程:https://jsfiddle.net/Lnpmbpcr/

关于vue.js - VueJS : Textarea input binding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44995186/

相关文章:

javascript - 在 Nuxt.js 中完全呈现路由后运行函数

javascript - 使用 :class 应用类

vue.js - 仅在 VueJS 中完成分派(dispatch)和提交后才触发路由

html - 背景图像及其子元素的不透明度问题

vue.js - Vue 动态属性

javascript - 如何在 Vue.js 中传递 $router.push 中的数据?

javascript - 将函数作为 props 传递给子组件

html - CSS:Vue.js 中的固定边栏

vue.js - v-for循环动态表达式中的Vuejs指令

javascript - Vue JS 路由器组件 - 在路由之间导航后无法重新使用使用 webpack 导入的 JS 文件