简体   繁体   English

如何在 vue.js 组件中动态添加属性

[英]How to dynamically add attributes in vue.js components

I am making a news website using NYT REST API.我正在使用 NYT REST API 制作一个新闻网站。 I want to add attributes to the components dynamically using the document.getElementsByClassName()我想使用document.getElementsByClassName()动态地向组件添加属性

For Example, I want to add a title to my component (the title is a prop actually of type String), I want to add it dynamically using a loop.例如,我想为我的组件添加一个标题(标题实际上是一个字符串类型的道具),我想使用循环动态添加它。 I have added it normally in the given code snippet, but what if I have 30 components and I have data in an array and I want to give title from that array.我已经在给定的代码片段中正常添加了它,但是如果我有 30 个组件并且我有一个数组中的数据并且我想从该数组中给出标题怎么办。

<News_Card class="card" :title='this.all_data[0].title' />

I want this :title='this.all_data[0].title to be added using loop我希望使用循环添加:title='this.all_data[0].title

You could use v-for directive to render the all_data items :您可以使用v-for指令来呈现all_data项:

<News_Card class="card" v-for="(item,index) in all_data" 
                    :title='item.title' :key="index" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM