Vue.js 带有对象双向数据绑定的Vuejs计算Setter

Vue.js 带有对象双向数据绑定的Vuejs计算Setter,vue.js,wysiwyg,quill,Vue.js,Wysiwyg,Quill,下面是两种数据绑定方式的基本示例: <template> <input v-model="fullname"/> <input v-model="first"/> <input v-model="last"/> </template> <script> var app = new Vue({ el: '#app', data: { first: '', last

下面是两种数据绑定方式的基本示例:

<template>
<input v-model="fullname"/>
<input v-model="first"/>
<input v-model="last"/>
</template>
<script>
    var app = new Vue({
      el: '#app',
      data: {
        first: '',
        last: ''
      },
      computed: {
        fullname: {
          // getter function
          get: function() {
            return this.first + " " + this.last
          },
          // setter function
          set: function(value) {
            var name = value.split(' ')
            this.first = name[0]
            this.last = name[1]
          }
        }
      }
    })
</script>
接下来,我通过computed对此进行了一点修改-这里只需添加到每个项目的
  • 标记:

    modifyArrayItem () {
      var output=''
        for (var i=0; i<this.myObject.length; i++) {
          if (this.myObject[i].active) {
            this.myObject[i].textOutput= '<li>' + this.myObject[i].text + '</li>'
            output = output + this.myObject[i].textOutput
          }
          else {
            this.myObject[i].textInput = ''
          }          
        }
        return output
      }, 
    
    modifyArrayItem(){
    变量输出=“”
    
    对于(var i=0;i是的,这是可能的。您可以将HTML解析为行,并在
    myObject
    中将行分配给相应的条目

    但是,请注意,每次键入内容时更新编辑器的内容会导致光标移动到编辑器的开头

    事实证明,quill有一种获取和设置选择位置的方法。它会变得有点毛茸茸的,因为每次编辑都会调用set两次,所以您必须检查是否有任何内容在实际更改。但这是有效的:

    modifyArrayItem: {
      get() {
        return this.myObject.map(o => "<li>" + o.text + "</li>").join("");
      },
      set(newValue) {
        const oldLines = this.myObject.map((o) => o.text);
        const lines = newValue.split("</p>")
          .map((p) => p.replace('<p>', ''));
        const same = oldLines.join('') === lines.join('');
    
        if (!same) {
          const pos = this.$refs.quill.quill.getSelection();
          for (let i=0; i < this.myObject.length; ++i) {
            this.myObject[i].text = lines[i];
          }
          this.$nextTick(() => {
            this.$refs.quill.quill.setSelection(pos);
          });
        }
      }
    }
    
    修改阵列项目:{
    得到(){
    返回此.myObject.map(o=>“
  • ”+o.text+“
  • ”).join(“”); }, 设置(新值){ constoldlines=this.myObject.map((o)=>o.text); 常量行=newValue.split(“

    ”) .map((p)=>p.replace(“”,”); const same=oldLines.join(“”)==lines.join(“”); 如果(!相同){ const pos=this.$refs.quill.quill.getSelection(); for(设i=0;i{ 此.$refs.quill.quill.setSelection(位置); }); } } }

    您能提供一些东西吗?它看起来像是
    modifyArrayItem()
    返回一个字符串。那么您想如何访问它的第n个元素?什么是
    proTemplate
    ?现在我只能告诉您使用
    sync
    修饰符或事件。查找如何在文档中使用它们。@on很抱歉,我现在已将其更正为
    myObject
    。这是如何访问第n个元素的主要问题。我认为它可以通过引用
    itemId
    来完成,这是唯一的。我想以某种方式与第一个示例类似。同步或事件可能是有价值的建议,我将寻找它们。最简单的解决方案是传递一个实际数组,并将其转换为编辑器组件内的字符串。因此,您可以键入ID并将其发送回编辑器组件Ondodoes,你是指很多编辑器吗?每一个都在单组件模板上?每一个都有一个代码< > LI> <代码>?或者类似的东西?我认为这是一个最后的解决方案,因为我的用户将不能在编辑器/ Li之间移动光标,可能会添加一个带有ITEID值的不可见标签到QuiLead编辑器?让我看看…Cursor移到开头可能会消除此解决方案。我需要它像正常键入一样工作。另一个想法是为每个
  • 在同一页面中依次放置5个羽毛笔编辑器,但在它们之间移动光标会有问题谢谢,你是我的StackOverflow英雄:D
    <div  v-html="modifyArrayItem"></div>
    
    <quill-editor v-model="modifyArrayItem" :options="options"></quill-editor>
    
    modifyArrayItem: {
      get() {
        return this.myObject.map(o => "<li>" + o.text + "</li>").join("");
      },
      set(newValue) {
        const oldLines = this.myObject.map((o) => o.text);
        const lines = newValue.split("</p>")
          .map((p) => p.replace('<p>', ''));
        const same = oldLines.join('') === lines.join('');
    
        if (!same) {
          const pos = this.$refs.quill.quill.getSelection();
          for (let i=0; i < this.myObject.length; ++i) {
            this.myObject[i].text = lines[i];
          }
          this.$nextTick(() => {
            this.$refs.quill.quill.setSelection(pos);
          });
        }
      }
    }