在同一页面上的多个编辑器上更新现有内容

时间:2019-04-11 12:33:32

标签: vue.js quill

我正在通过VueJS应用程序中的自定义包装器组件使用羽毛笔。因此,问题是,在打开用于编辑问题的模态及其在两个不同的编辑器上的答案时,会出现“笔芯无效笔芯容器#my_id”错误。 其次是, “ nextTick中的错误:“ TypeError:无法设置未定义的属性'innerHTML'”

这是包装程序的代码:

import Quill from 'quill'
export default {
  name: 'QuillWrapper',
  props: {
    value: {
      type: String,
      default: ''
    },
    id: {
      type: String,
      default: 'editor'
    }
  }, 
  data() {
    return {
        editor: null,
        quillToolbar: [
          ['bold', 'italic', 'underline', 'strike'],
          ['blockquote', 'code-block', 'link'],

          [{ 'list': 'ordered'}, { 'list': 'bullet' }],
          [{ 'script': 'sub'}, { 'script': 'super' }],
          [{ 'indent': '-1'}, { 'indent': '+1' }],
          [{ 'direction': 'rtl' }],

          ['image', 'video'],

          [{ 'size': ['small', false, 'large', 'huge'] }],
          [{ 'header': [1, 2, 3, 4, 5, 6, false] }],

          [{'color': ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'white', 'black']}, {'background': ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'white', 'black']}],
          [{ 'font': [] }],
          [{ 'align': [] }],

          ['clean', 'formula']
        ],
    };
  },
  mounted() {
    this.$nextTick(function() {
      this.editor = new Quill('#' + this.id, {
        modules: {
          toolbar: this.quillToolbar
        },
        bounds: '.text-editor',
        theme: 'snow',
        syntax: true
      });  
      this.editor.root.innerHTML = this.value;  
      this.editor.on('text-change', () => this.update());
    })
  },

  methods: {
    update() {
      this.$emit('input', this.editor.getText() ? this.editor.root.innerHTML : '');
    }
  }
}

1 个答案:

答案 0 :(得分:0)

之所以不起作用,是因为element中的DOM不了解id或实际上没有得到分配。对于DOM,属性this.id只不过是string

您必须在element期间为容纳组件的id分配一个mounted()。此外,请确保要导入snow主题.css文件..

<link rel="stylesheet" href="https://cdn.quilljs.com/1.3.6/quill.snow.css">


下面是代码段,您还可以在这里找到CodePen镜像:

https://codepen.io/oze4/pen/wZdjbv?editors=1010


这是更改的地方:

mounted() {

    /* THIS WAS ADDED */
    this.$el.id = this.id;
    /* ^^^^^^^^^^^^^^ */

    this.$nextTick(function() {
      this.editor = new Quill("#" + this.id, {
        modules: {
          toolbar: this.quillToolbar
        },
        bounds: ".text-editor",
        theme: "snow",
        syntax: true
      });
      this.editor.root.innerHTML = this.value;
      this.editor.on("text-change", () => this.update());
    });
  },

const quillComponent = {
  template: "#quillComponent",
  props: {
    value: {
      type: String,
      default: ""
    },
    id: {
      type: String,
      default: "editor"
    }
  },
  data: {
    editor: null,
    quillToolbar: [
      ["bold", "italic", "underline", "strike"],
      ["blockquote", "code-block", "link"],
      [{ list: "ordered" }, { list: "bullet" }],
      [{ script: "sub" }, { script: "super" }],
      [{ indent: "-1" }, { indent: "+1" }],
      [{ direction: "rtl" }],
      ["image", "video"],
      [{ size: ["small", false, "large", "huge"] }],
      [{ header: [1, 2, 3, 4, 5, 6, false] }],
      [
        {color: ["red","orange","yellow","green","blue","purple","white","black"]},
        {background: ["red","orange","yellow","green","blue","purple","white","black"]}
      ],
      [{ font: [] }],
      [{ align: [] }],
      ["clean", "formula"]
    ]
  },
  mounted() {
    /* THIS WAS ADDED */
    this.$el.id = this.id;
    /* ^^^^^^^^^^^^^^ */
    this.$nextTick(function() {
      this.editor = new Quill("#" + this.id, {
        modules: {
          toolbar: this.quillToolbar
        },
        bounds: ".text-editor",
        theme: "snow",
        syntax: true
      });
      this.editor.root.innerHTML = this.value;
      this.editor.on("text-change", () => this.update());
    });
  },
  methods: {
    update() {
      let r = this.editor.getText() ? this.editor.root.innerHTML : "";
      console.log(r);
      this.$emit(
        "input",
        this.editor.getText() ? this.editor.root.innerHTML : ""
      );
    }
  }
};

new Vue({
  el: "#app",
  components: {
    quillComponent,
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/quill/1.3.6/quill.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>

<link rel="stylesheet" href="https://cdn.quilljs.com/1.3.6/quill.snow.css">


<div id="app">
  <quill-component></quill-component>
</div>

<script type="text/x-template" id="quillComponent">
  <div></div>
</script>