Search code examples
vue.jsvuejs2quill

How you do you call a method once rendering is done in Vue?


I have am building a Vue app that includes a QuillJS editor in a tab. I have a simple setTab(tabName) Vue method that shows/hides tabs with the v-if directive.

methods: {
  setTab: function (tabName) {
    this.view = tabName;
    if(tabName === 'compose') {
      var editor = new Quill('#editor', {
        modules: { toolbar: '#toolbar' },
        theme: 'snow'
      });
    }
  }
}

My tab is basically like this:

<div id="composer" v-if="tabName === 'compose'">
  <!-- toolbar container -->
  <div id="toolbar">
    <button class="ql-bold">Bold</button>
    <button class="ql-italic">Italic</button>
  </div>
  <!-- editor container -->
  <div id="editor">
    <p>Hello World!</p>
  </div>
</div>

Currently, I'm getting an error because the #editor element does not yet exist when I am calling new Quill(...). How do I delay that QuillJS initialization on the page so that it doesn't happen until after the #editor is already there?


Solution

  • Use this.$nextTick() to defer a callback to be executed after the next DOM update cycle (e.g., after changing a data property that causes a render-update).

    For example, you could do this:

    methods: {
      setTab: function (tabName) {
        this.view = tabName;
        if(tabName === 'compose') {
          this.$nextTick(() => {
            var editor = new Quill('#editor', {
              modules: { toolbar: '#toolbar' },
              theme: 'snow'
            });
          })
        }
      }
    }