I need to insert a compiled element into the DOM
, however it will be inserted into a random place, not in a pre-defined location as is documentation…
JavaScript
x
9
1
var res = Vue.compile('<div><span>{{ msg }}</span></div>')
2
new Vue({
3
data: {
4
msg: 'hello'
5
},
6
render: res.render,
7
staticRenderFns: res.staticRenderFns
8
})
9
All approaches with V-for
, V-if/show
will not serve as they also require predefined elements.
I tried something like this…
JavaScript
1
2
1
document.getElementById('elPai').insertAdjacentHTML('beforeend', Vue.compile('<div><span>{{ msg }}</span></div>'));
2
It returns an object containing ‘ render ‘ and ‘ StaticRenderFns ‘, but did not find the result compiled on these objects, it seems to me that it is recorded in a ‘ Promisse ‘, which is triggered when the element is predefined in ‘ DOM ‘.
Finally, there is how to insert elements compiled into DOM with ‘ Vue 2 ‘?
Advertisement
Answer
Try this
JavaScript
1
6
1
var MyComponent = Vue.extend({
2
template: '<div>Hello!</div>'
3
})
4
5
new MyComponent().$mount('#app')
6
Example from: https://v2.vuejs.org/v2/api/#vm-mount