# Keep pre-existing DOM alive in Vue For my use cases, Vue has one critical pitfall: I frequently have/want to use Vue components with ``s as wrappers for content from a CMS which I don't have control over. That is, the content comes over the wire via HTML, and I have to activate Vue for some of it. > ```html > >

Slot content I don't have control over

>
> ``` > *I need to activate the Vue component ``.* In particular, "slot content I don't have control over" may include interactive elements with attached event listeners etc. This content will be re-rendered when activating Vue and subsequently, all previous modifications to its DOM — including any DOM references and event listeners — will be lost: ```javascript document.querySelector('p').onclick = function () { alert('Hello world!') } // Clicking the

will greet the world new Vue({ el: 'interactive-element' }) // Clicking the

will no longer do anything ``` That's where this script comes in. It exposes a `DomKeepAlive` class and an according `` Vue component. Unfortunately, just a Vue component won't do the trick, so the activation routine is a little more tricky: 1. Add `` wrappers to the HTML we have control over (the wrapper): ```html

Slot content I don't have control over

``` 2. Add some action before and after throwing Vue onto the ``: ```javascript // Somewhere in your script const dka = new DomKeepAlive(Vue) // For each of the Vue instances to activate: // Pick the element that holds the future Vue instance const el = document.querySelector('interactive-element') // Get an initializer for the nodes *before* activating Vue const init = dka.prepareRenderTarget(el) // Activate Vue const vm = new Vue({ el, // ... }) // Run the initializer on the Vue root node init(vm.$el) ``` This will prevent the content inside the `` from ever being re-rendered by Vue. Some more things to note: * The script uses MutationObservers to detect entrance or removal of the contained nodes, so it works even when a `` lives inside a `v-if` branch of the surrounding component. * You may have multiple `` wrappers inside the same Vue component. However, don't nest them. * `` content is pretty much handled like `v-pre` — you cannot use interactive Vue syntax inside it. * An actual DOM element needs to be rendered in place of a `` placeholder. By default, this will be a `
`, but you can easily change that with the familiar Vue syntax: ``.