【发布时间】:2020-06-28 02:27:24
【问题描述】:
我正在尝试使用 Vuejs ref 属性来定位一些元素,以随机化每个页面加载的顺序。
我的数据在模板中渲染并在组件中处理:
<div class="team" >
<div class="team__card" ref="card" v-for="(member, index) in team"
:key="index">
<div v-for="(image, imageIndex) in member.image" :key="imageIndex">
<img :src="image" alt="Photo of team member" :key="imageIndex" />
</div>
<div class="team__card-content">
<p class="font-bold text-xl">{{ member.name }}</p>
<p class="font-bold text-gray-700">{{ member.title }}</p>
<p class="text-gray-700">{{ member.description }}</p>
</div>
</div>
</div>
<script>
export default {
name: 'Page Name',
data() {
return {
team: [
{
image: [require('url')],
name: 'Name',
title: 'Title',
description:'description.'
},
{
image: [require('url')],
name: 'Name',
title: 'Title',
description:'description.'
},
]
}
},
created() {
this.randomize()
},
methods: {
randomize() {
for (let i = this.team.length - 1; i > 0; i--) {
let randomIndex = Math.floor(Math.random() * i)
let temp = this.team[i]
this.set(this.team, i, this.team[randomIndex])
this.set(this.team, randomIndex, temp)
}
}
}
}
</script>
我在这里缺少什么?我如何在我的卡片元素 TIA 的每个页面加载上洗牌/随机排序!
【问题讨论】:
-
你在
v-for中有card引用吗(即有多个实例)?如果是这样,我相信$refs.card将是一个数组,因此不会有eq方法 -
您的浏览器控制台中是否有任何错误消息?
-
没有错误信息!
-
我相信如果你想要一个引用数组,你必须使用
v-for -
好的,我会看看如何做到这一点。目前我的数据结构在模板中处理。我知道如何在 js 或 jquery 中定位这些,但不知道 DOM 操作如何在 vuejs 中工作
标签: javascript arrays vue.js dom ref