Javascript 尝试使用v-for循环和对象属性动态创建URL,但出现GET错误
一个对象有一些属性,其中6个被称为Javascript 尝试使用v-for循环和对象属性动态创建URL,但出现GET错误,javascript,vue.js,vuejs2,Javascript,Vue.js,Vuejs2,一个对象有一些属性,其中6个被称为item0、item1、item2、item3、item4和item5。我想使用URL中的这些属性值和我看到它的方式呈现6个图像,我可以复制图像元素6次,只需更改项目编号,如下所示: <img v-if='match.mainParticipant.stats.item0' :src="'http://ddragon.leagueoflegends.com/cdn/' + $store.getters.version + '/img/item/' + ma
item0、item1、item2、item3、item4和item5
。我想使用URL中的这些属性值和我看到它的方式呈现6个图像,我可以复制图像元素6次,只需更改项目编号,如下所示:
<img v-if='match.mainParticipant.stats.item0' :src="'http://ddragon.leagueoflegends.com/cdn/' + $store.getters.version + '/img/item/' + match.mainParticipant.stats.item0 + '.png'" alt="">
<img v-if='match.mainParticipant.stats.item1' :src="'http://ddragon.leagueoflegends.com/cdn/' + $store.getters.version + '/img/item/' + match.mainParticipant.stats.item1 + '.png'" alt="">
<img v-if='match.mainParticipant.stats.item2' :src="'http://ddragon.leagueoflegends.com/cdn/' + $store.getters.version + '/img/item/' + match.mainParticipant.stats.item2 + '.png'" alt="">
<img v-if='match.mainParticipant.stats.item3' :src="'http://ddragon.leagueoflegends.com/cdn/' + $store.getters.version + '/img/item/' + match.mainParticipant.stats.item3 + '.png'" alt="">
<img v-if='match.mainParticipant.stats.item4' :src="'http://ddragon.leagueoflegends.com/cdn/' + $store.getters.version + '/img/item/' + match.mainParticipant.stats.item4 + '.png'" alt="">
<img v-if='match.mainParticipant.stats.item5' :src="'http://ddragon.leagueoflegends.com/cdn/' + $store.getters.version + '/img/item/' + match.mainParticipant.stats.item5 + '.png'" alt="">
<img v-for="i in 6" :src="'http://ddragon.leagueoflegends.com/cdn/' + $store.getters.version + '/img/item/' + match.mainParticipant.stats.item + (i - 1) + '.png'" alt="">
显然,为了使其正常工作,未定义的4应该是item4,但我不明白为什么URL会变成这样。当您访问名为
match.mainparticle.stats.item
的属性时,它的编写方式将是未定义的。然后在该值的末尾添加一个数字,得到undefined4
,等等
您需要使用方括号来动态访问属性
因此:
match.mainpactor.stats['item'+(i-1)]
或使用反勾号:
match.mainpactor.stats[`item${i-1}`]
GET http://ddragon.leagueoflegends.com/cdn/9.16.1/img/item/undefined4.png 403 (Forbidden)