Vuejs 选择动态(多个)元素 [英] Vuejs select dynamic (multiple) elements
问题描述
我将 VueJS 与 PHP 应用程序一起使用.我有多个帖子,每个帖子都有评论.他们每个人都有一个类answer_comment
.我正在尝试让 VueJS 对所有这些都有效(它目前只对一个有效).
I am using VueJS with a PHP application. I have multiple posts with comments for each of them. Each of them has a class answer_comment
. I am trying to make VueJS work for all of them (it currently only works with one).
new Vue({ el: '.answer_comment', /* other code */ })
这只影响第一个.我怎样才能让它对所有人都有效?如何选择具有 answer_comment
类的所有元素而不是只获取第一个元素?
This only affects the first one. How can I make it work for all of them? How can I select all elements with the class of answer_comment
instead of only getting the first one?
推荐答案
UPDATE:既然给出了这个答案,Vue 现在不允许在 body
标签上安装 VM.所以,在它下面使用一些元素.
UPDATE: Since this answer was given, Vue now disallows mounting a VM on the body
tag. So, use some element beneath it.
使用 new Vue
方法实际上只是设置一个具有自己层次结构的视图模型.它最终只会匹配与您的 CSS 选择器匹配的第一个 DOM 元素.
Using the new Vue
approach is really just setting up a single View Model with its own hierarchy. It will end up just matching the first DOM element that matches your CSS selector.
在这种情况下,我建议将您的 new Vue
移至 body
标记并为其声明一个answer_comment"组件,然后 Vue 将在整个 body
当它编译"它时.
In this case, I recommend moving your new Vue
up to the body
tag and declare an "answer_comment" component for it that Vue will then apply throughout the body
when it "compiles" it.
查看组件文档了解更多信息:
See the component documentation for more information:
http://vuejs.org/guide/components.html
这篇关于Vuejs 选择动态(多个)元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!