vue如何在for循環(huán)中設(shè)置ref并獲取$refs
一、單循環(huán)動態(tài)設(shè)置ref
1.設(shè)置:【:ref=“‘XXX’ + index”】XXX -->自定義ref的名字
2.獲取:let ref = eval(‘this.$refs.XXX’ + index)[0]
3.示例:

代碼如下所示
<template>
<div class="ref_test">
<div v-for="(item, index) in dataList" :key="index" :ref="'refName' + index" @click="clickGetRef(index)">
<p>{{ item.title }}</p>
</div>
</div>
</template>
<script>
export default {
data() {
return {
dataList: [
{
"id": 1,
"title": "這是來測試如何獲取動態(tài)ref的"
},
{
"id": 2,
"title": "第2條數(shù)據(jù)"
},
{
"id": 3,
"title": "第3條數(shù)據(jù)"
},
{
"id": 4,
"title": "第4條數(shù)據(jù)"
},
]
}
},
methods: {
clickGetRef(index) {
let ref = eval('this.$refs.refName' + index)[0]
console.log(ref);
}
},
}
</script>
<style></style>二、雙循環(huán)動態(tài)設(shè)置ref
1.設(shè)置:【:ref=“‘XXX’ + (index+i)”】或者【:ref=“‘XXX’ + id”】
index+i -->兩個for循環(huán)的索引;
id -->item的唯一標(biāo)識;
2.獲取:
let ref = eval('this.$refs.XXX' + (index + i))[0]
或者
let ref = eval('this.$refs.XXX' + (item.id))[0]
3.示例:

代碼如下所示
<template>
<div>
<div class="ref_double_test">
<div v-for="(item, index) in dataLists" :key="index">
<div class="content" v-for="(sonItem, i) in item.sonList" :key="index + i" @click="clickGetDoubleRef(index, i, sonItem)">
<!-- 方式一:用索引的方式,用一個索引可能會重復(fù),為防止重復(fù),則用兩個索引【index + i】 -->
<div :ref="'refName1' + (index + i)">{{ item.title }}</div> ----
<!-- 方式二:用id的方式 -->
<div :ref="'refName2' + sonItem.sonId">{{ sonItem.sonContent }}</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
dataLists: [
{
"id": 1,
"title": "第1條數(shù)據(jù)",
"sonList": [
{ "sonId": 1, "sonContent": "子集第1條數(shù)據(jù)" },
{ "sonId": 2, "sonContent": "子集第2條數(shù)據(jù)" },
]
},
{
"id": 2,
"title": "第2條數(shù)據(jù)",
"sonList": [
{ "sonId": 11, "sonContent": "子集第11條數(shù)據(jù)" },
{ "sonId": 22, "sonContent": "子集第22條數(shù)據(jù)" },
]
},
{
"id": 3,
"title": "第3條數(shù)據(jù)",
"sonList": [
{ "sonId": 111, "sonContent": "子集第111條數(shù)據(jù)" },
{ "sonId": 222, "sonContent": "子集第222條數(shù)據(jù)" },
]
}
]
}
},
methods: {
clickGetDoubleRef(index, i, sonItem) {
// 方式一
let ref1 = eval('this.$refs.refName1' + (index + i))[0]
console.log('ref1', ref1);
// 方式二
let ref2 = eval('this.$refs.refName2' + sonItem.sonId)[0]
console.log('ref2', ref2);
}
},
}
</script>
<style>
.ref_test {
width: 500px;
height: 100px;
border: 1px solid gray;
}
.content {
width: 500px;
height: 30px;
display: flex;
background: rebeccapurple;
margin-bottom: 10px;
}
</style>
總結(jié)
到此這篇關(guān)于vue如何在for循環(huán)中設(shè)置ref并獲取$refs的文章就介紹到這了,更多相關(guān)vue循環(huán)設(shè)置ref并獲取$refs內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue動態(tài)組件實(shí)現(xiàn)選項卡切換效果
這篇文章主要為大家詳細(xì)介紹了vue動態(tài)組件實(shí)現(xiàn)選項卡切換效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
vue微信分享出來的鏈接點(diǎn)開是首頁問題的解決方法
這篇文章主要為大家詳細(xì)介紹了vue微信分享出來的鏈接點(diǎn)開是首頁問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
去除element-ui下拉框的下拉箭頭的實(shí)現(xiàn)
我們最開始拿到的element-ui是帶有下拉箭頭的,那么如何去除element-ui下拉框的下拉箭頭的實(shí)現(xiàn),本文就詳細(xì)的介紹一下,感興趣的可以了解一下2023-08-08
關(guān)于vuex強(qiáng)刷數(shù)據(jù)丟失問題解析
這篇文章主要介紹了關(guān)于vuex強(qiáng)刷數(shù)據(jù)丟失問題解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
vue.js實(shí)現(xiàn)左邊導(dǎo)航切換右邊內(nèi)容
這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)左邊導(dǎo)航切換右邊內(nèi)容,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10
Vue?element-ui?el-cascader?只能末級多選問題
這篇文章主要介紹了Vue?element-ui?el-cascader?只能末級多選問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

