Answer the question
In order to leave comments, you need to log in
How to correctly initialize a Vue component after an element appears in the DOM?
I have a page that has a button that opens a modal window.
The content of the modal window is loaded via AJAX and the content of the response (HTML) is inserted into the DOM.
This HTML contains some Vue components (comment component).
How to correctly instantiate this Vue component?
import Vue from "vue";
import Comments from "./components/comments.vue";
import {listenDomForElement} from "./../../common/helpers";
'use strict';
/** Variables */
let commentsBlockVm,
vueElement = '#comments-block';
/** Комментарии */
commentsBlockVm = new Vue({
name: 'comments-block',
components: {Comments},
});
/** Специальный хелпер, слушающий DOM (MutationObserver) */
listenDomForElement(vueElement, function (element) {
// Коллбэк исполнится, когда элемент появится в DOM
// Маунтим Vue инстанс к нужному элементу
commentsBlockVm.$mount(element);
// Без этого компонент нормально не работает
window.commentsBlockVm = commentsBlockVm;
});
export {commentsBlockVm}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question