Quill Editor won't display v-model in input field (Vue 3)

4.8k Views Asked by At

I want to display some html, fetched from a database inside of quill editor. The html seems to be fine (displayed in <p> paragraph) and is bound to quill editor via v-model but it just does not get displayed:

<template>
  <div id="text-editor" class="text-editor">
    <quill-editor :modules="modules" :toolbar="toolbar" v-model:content="store.re.body" contentType="html"/>
    <p>{{store.re.body}}</p>
  </div>
</template>


<script setup>
import BlotFormatter from 'quill-blot-formatter'
import store from "../../../js/store";

store.re.body = ''

const modules = {
    module: BlotFormatter,
}

const toolbar = [
    [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
    [{ 'size': ['small', false, 'large', 'huge'] }],
    ['bold', 'italic', 'underline', 'strike'],
    ['blockquote', 'code-block'],
    [{ 'align': [] }],
    [{ 'list': 'ordered'}, { 'list': 'bullet' }],
    [{ 'color': [] }, { 'background': [] }],
    [{ 'font': [] }],
    ['link', 'image', 'video'],
    ['clean']
];
</script>

This is where the data gets fetched from the database (inside another vue component):

axios.get('/get-blog', {
        params: {
            id: state.id
        }
    })
    .then(res => {
        state.body = store.re.body = res.data[0].body
    })
    .catch(err => {
        state.error = true
        setTimeout(() => state.error = false, 5000)
    })

I am using store.re.body (reactive store) to transport it to quill editor.

store.js:

import {reactive} from "vue";

const re = reactive({})

export default {
    re
}

Here you can see the editor page displayed, below with working <p> paragraph but the editor input stays empty:

enter image description here

4

There are 4 best solutions below

0
tony19 On BEST ANSWER

The quill-editor component does not watch props.content (vueup/vue-quill#35). The watch was removed in an attempt to fix another bug, where the cursor would reset to the beginning of the line.

As a workaround, add your own watch on content, and call quill-editor's setHTML() with the new value. However, you'll need to move the cursor to the end of the line (using Quill's setSelection()) to workaround the bug mentioned above:

<template>
  <quill-editor ref="quill" v-model:content="content" ⋯/>
</template>

<script setup>
import store from '@/store'
import { watch, ref, nextTick } from 'vue'

const content = ref('')
const quill = ref(null)
let newContent = ''

watch(content, newValue => {
  newContent = newValue
  store.re.body = newValue
})

watch(
  () => store.re.body,
  newValue => {
    if (newContent === newValue) return

    quill.value.setHTML(newValue)

    // Workaround https://github.com/vueup/vue-quill/issues/52
    // move cursor to end
    nextTick(() => {
      let q = quill.value.getQuill()
      q.setSelection(newValue.length, 0, 'api')
      q.focus()
    })
  }
)

⋮
</script>

<template>
  <quill-editor ref="quill" ⋯/>
</template>

demo

1
Sharif On

you should try with these example bellow

    <script>
     import {QuillEditor} from "@vueup/vue-quill";
     export default {
       components: {
         QuillEditor, 
       },
 data(){
    return {
     someText:''
   }
}, 
   computed: {
     editor() {
      return this.$refs.quillEditor;
     },
   },
   methods: {
     getSetText() {
       this.someText = "<div><p>this is some text</p> </div>";
       this.editor.setHTML(this.someText);
      },
     }, 
   }
 </script>
    <template><quill-editor ref="quillEditor" contentType="html"v-model:content="someText" theme="snow" ></quill-editor></template>
    

0
Makerovski On

I had this problem when getting content from a data, I solved adding this on mounted:

 document.querySelector(".ql-editor").innerHTML = item.value.content

I hope it helps. Thx

0
Botea Florin On

for me, using

v-model:value="form.html" @input="val => form.html= val"

worked... I think they not yet comply to new Vue3 model standards