javascript - Using generic array name in template
问题描述
I am really new to vue.js and HTML. I have two components that are working together. A photo-gallery and a tag-component. The tag-component uses the tags array to get the tags for a image. What i would like is to have a tag array for each image. I figured i could use the imageIndex
and append that to the array name so I was string something like :tags=tags+imageIndex
.
But if I do that, it does not work (i think it is not recognized as an array any more...) . My question is, is there a way to enter that in to the template, so that i have for each image an array with the name tags{index}
?
Many thanks for the help!
Robin
<div id="SingleFile">
<button
id="refreshbtn"
class="btn btn-primary btn-margin"
@click="updateFileList">
refresh
</button>
<gallery :images="images" :index="index" @close="index = null"></gallery>
<div
:key="imageIndex"
:style="{ backgroundImage: 'url(' + image + ')', width: '300px', height: '200px' }"
@click="index = imageIndex"
class="image"
v-for="(image, imageIndex) in images"
>
<div>
<vue-tags-input
v-model="tag"
:tags="tags"
@tags-changed="newTags => tags = newTags"
/>
</div>
</div>
<div class="upload">
<upload-image url="http://localhost:8080/user" name="files" max_files="100"></upload-image>
</div>
</div>
解决方案
您可以使用一种方法来实现:
<vue-tags-input
v-model="tag"
:tags="getTags(imageIndex)" ... >
并将其定义为:
methods:{
getTags(i){
return this['tags'+i];
}
}
通过假设您的数据对象中有类似的内容:
data(){
return{
tags0:[],
tags1:[],
...
}
}
或任何具有该语法的属性tags+index
推荐阅读
- javascript - 在 ReactJS 中删除 onClick 元素?
- javascript - 重新加载时显示角度模态?
- sql - SQL Server:求和值并从两个不同的表中找到差异
- openstack - OpenStack VM File System Error on reboot and nova rescue 无法拯救卷支持的实例 (HTTP 400)
- android - android pip 模式和活动导航
- bash - 针对文件按顺序运行带有 JSON 有效负载的 Curl POST
- java - Sceneform ARCore 加载并构建随机 3D 资产
- python - 使用 astropy.modeling 拟合具有负幅值的高斯问题
- sql-server - SQL Server 查询失败
- sql - 如何在 Oracle 11.g 中正确选择中间的行数?