如何将Quill Text Editor用作vue.js中的组件

发布于 2025-01-29 16:55:02 字数 1511 浏览 7 评论 0原文

我在这里看到的是VUE中的Quill Text Editor作为组件:

<template>
  <div ref="editor"></div>
</template>
<script>
import Quill from 'quill';
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.bubble.css';
import 'quill/dist/quill.snow.css';

export default {
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      editor: null
    };
  },
  mounted() {
    var _this = this;

    this.editor = new Quill(this.$refs.editor, {
      modules: {
        toolbar: [[{
          header: [1, 2, 3, 4, false]
        }], ['bold', 'italic', 'underline', 'link']]
      },
      //theme: 'bubble',
      theme: 'snow',
      formats: ['bold', 'underline', 'header', 'italic', 'link'],
      placeholder: "Type something in here!"
    });
    this.editor.root.innerHTML = this.value;
    this.editor.on('text-change', function () {
      return _this.update();
    });
  },

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

因此,它是基本的Quill组件,我使用Quill 1.3.7。对于文档: https://quilljs.com/

因此,在父母的组件中,我创建了V-Model,以查看以查看此组件的结果:

<text-editor
                    v-model="model"/>
                <p> <span v-html="model"></span></p>

但是不幸的是,我没有任何响应,也没有错误。您认为我做错了什么?

I created quill text editor as a component in vue as you can see here:

<template>
  <div ref="editor"></div>
</template>
<script>
import Quill from 'quill';
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.bubble.css';
import 'quill/dist/quill.snow.css';

export default {
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      editor: null
    };
  },
  mounted() {
    var _this = this;

    this.editor = new Quill(this.$refs.editor, {
      modules: {
        toolbar: [[{
          header: [1, 2, 3, 4, false]
        }], ['bold', 'italic', 'underline', 'link']]
      },
      //theme: 'bubble',
      theme: 'snow',
      formats: ['bold', 'underline', 'header', 'italic', 'link'],
      placeholder: "Type something in here!"
    });
    this.editor.root.innerHTML = this.value;
    this.editor.on('text-change', function () {
      return _this.update();
    });
  },

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

So it is a basic quill component and I use quill 1.3.7. For the documentation: https://quilljs.com/

So in the parent component I created v-model to see the result of this component:

<text-editor
                    v-model="model"/>
                <p> <span v-html="model"></span></p>

But unfortunately I dont get any response and no error either. What do you think I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筑梦 2025-02-05 16:55:03

即使您将问题标记为a vue 2 问题,我猜想您正在使用 vue 3 ,因为否则,它应该可以正常工作(我也尝试了您的代码并将其修复在Vue 3)。
您实际上是在面对破坏变化问题。 >
现在要使用V模型更新数据时,需要使用:

  • prop: value -&gt; ModelValue;
  • 事件:输入 - &gt;更新:modelValue;

您的代码应该看起来像这样!

<template>
  <div ref="editor"></div>
</template>
<script>
import Quill from "quill";
import "quill/dist/quill.core.css";
import "quill/dist/quill.bubble.css";
import "quill/dist/quill.snow.css";

export default {
  props: {
    modelValue: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      editor: null,
    };
  },
  mounted() {
    var _this = this;

    this.editor = new Quill(this.$refs.editor, {
      modules: {
        toolbar: [
          [
            {
              header: [1, 2, 3, 4, false],
            },
          ],
          ["bold", "italic", "underline", "link"],
        ],
      },
      //theme: 'bubble',
      theme: "snow",
      formats: ["bold", "underline", "header", "italic", "link"],
      placeholder: "Type something in here!",
    });
    this.editor.root.innerHTML = this.modelValue;
    this.editor.on("text-change", function () {
      return _this.update();
    });
  },

  methods: {
    update: function update() {
      this.$emit(
        "update:modelValue",
        this.editor.getText() ? this.editor.root.innerHTML : ""
      );
    },
  },
};
</script>

对于VUE 3:
您的项目祝您好运,

Even though you tagged the question as a Vue 2 question, I am going to guess you are using Vue 3 because otherwise, it should work just fine (I also tried your code and fixed it on Vue 3).
You are actually facing a breaking change issue.
When you want to update your data with a v-model now, you need to use :

  • prop : value -> modelValue;
  • event : input -> update:modelValue;

Your code should look like this for Vue 3 :

<template>
  <div ref="editor"></div>
</template>
<script>
import Quill from "quill";
import "quill/dist/quill.core.css";
import "quill/dist/quill.bubble.css";
import "quill/dist/quill.snow.css";

export default {
  props: {
    modelValue: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      editor: null,
    };
  },
  mounted() {
    var _this = this;

    this.editor = new Quill(this.$refs.editor, {
      modules: {
        toolbar: [
          [
            {
              header: [1, 2, 3, 4, false],
            },
          ],
          ["bold", "italic", "underline", "link"],
        ],
      },
      //theme: 'bubble',
      theme: "snow",
      formats: ["bold", "underline", "header", "italic", "link"],
      placeholder: "Type something in here!",
    });
    this.editor.root.innerHTML = this.modelValue;
    this.editor.on("text-change", function () {
      return _this.update();
    });
  },

  methods: {
    update: function update() {
      this.$emit(
        "update:modelValue",
        this.editor.getText() ? this.editor.root.innerHTML : ""
      );
    },
  },
};
</script>

Good luck with your project!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文