【发布时间】:2018-04-25 16:52:43
【问题描述】:
我可以使用带有 Vue.js 实例的静态模板,如下所示。
firstPlaceholder 的内容被替换为模板staticTemplate 的内容,text 属性被正确呈现。
(可能需要 Chrome 才能正常工作。)
但是,如果我动态创建模板,渲染将不起作用。 secondPlaceholder 不见了。也许这是一个时间问题?
=>如何调整我的代码以使用 dynamicTemplate 呈现 secondPlaceholder?
<!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>
相关问题:
How to target custom element (native web component) in vue.js?
【问题讨论】:
标签: javascript templates vue.js