我正在使用 NYT REST API 制作一个新闻网站。我想使用 document.getElementsByClassName()
动态向组件添加属性例如,我想向我的组件添加一个标题(标题实际上是 String 类型的 prop),我想使用循环动态添加它。我通常已将其添加到给定的代码片段中,但如果我有 30 个组件,并且数组中有数据,并且我想从该数组中给出标题,该怎么办?
<News_Card class="card" :title='this.all_data[0].title' />
我希望使用循环添加此 :title='this.all_data[0].title
最佳答案
您可以使用 v-for
指令来渲染 all_data
项:
<News_Card class="card" v-for="(item,index) in all_data"
:title='item.title' :key="index" />
关于javascript - 如何在vue.js组件中动态添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69827533/