我的 php 应用程序中有一个自定义 vue 组件和一个为其呈现自定义元素的 View 。
<div class="articles">
{% for a in articles %}
<budget-article id="{{ a.id }}" name="{{ a.name }}" :amount="{{ a.amount }}" :enable="true"></budget-article>
{% endfor %}
</div>
在 ready
事件之后,我将组件添加到主 vue 实例,如下所示:
ready: function () {
this.$parent.articles.push(this);
}
所以,我有一个组件数组,但所有组件都已经渲染在页面上。
问题是如何向页面添加另一个文章组件?
我认为我可以使用没有模板的组件,并在准备好时将数据推送到主应用程序,但对我来说看起来有点奇怪。所以,我想也许有更好的解决方案。
最佳答案
您需要使用 Vue 来执行 for 循环,而不是服务器模板语言:
<div class="articles">
<budget-article v-for="article in articles" id="{{ article.id }}" name="{{ article.name }}" :amount="{{ article.amount }}" :enable="true"></budget-article>
</div>
然后,每当 articles
数组被推送到时,就会出现一篇新文章。
要最初将文章添加到页面,您应该执行以下操作(伪代码,因为我不知道模板语言):
data:function(){
return {
articles: {% json_encode(articles) %}
}
}
关于javascript - 添加vue组件动态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35578033/