I am able to use a static template with a Vue.js instance as shown below.
The content of the firstPlaceholder is replaced with the content of the template staticTemplate and the text property is rendered correctly.
(Might require Chrome to work correctly.)
However, if I create a template dynamically, the rendering does not work. The secondPlaceholder is missing. Maybe this is a timing issue?
=>How can I adapt my code to render the secondPlaceholder using the dynamicTemplate?
<!doctype html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<template id = "staticTemplate">
<div>{{text}}</div>
</template>
<div id ="firstPlaceholder"></div>
<div id ="secondPlaceholder"></div>
<script>
var dynamicTemplate = document.createElement('template');
dynamicTemplate.id = 'dynamicTemplate';
var div = document.createElement('div');
div.innerText = '{{text}}';
dynamicTemplate.appendChild(div);
var staticTemplate = document.getElementById('staticTemplate');
document.body.insertBefore(dynamicTemplate, staticTemplate);
new Vue({
el: '#firstPlaceholder',
template: '#staticTemplate',
data: {
text: 'My first text'
}
});
new Vue({
el: '#secondPlaceholder',
template: '#dynamicTemplate',
data: {
text: 'My second text'
}
});
</script>
</body>
</html>
Related question:
How to target custom element (native web component) in vue.js?