vue如何在for循環(huán)中設(shè)置ref并獲取$refs
一、單循環(huán)動(dòng)態(tài)設(shè)置ref
1.設(shè)置:【:ref=“‘XXX’ + index”】XXX -->自定義ref的名字
2.獲?。簂et 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": "這是來(lái)測(cè)試如何獲取動(dòng)態(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)動(dòng)態(tài)設(shè)置ref
1.設(shè)置:【:ref=“‘XXX’ + (index+i)”】或者【:ref=“‘XXX’ + id”】
index+i -->兩個(gè)for循環(huán)的索引;
id -->item的唯一標(biāo)識(shí);
2.獲?。?/p>
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)">
<!-- 方式一:用索引的方式,用一個(gè)索引可能會(huì)重復(fù),為防止重復(fù),則用兩個(gè)索引【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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue中for循環(huán)作用域問(wèn)題處理方式
- vue element-ui v-for循環(huán)el-upload上傳圖片 動(dòng)態(tài)添加、刪除方式
- vue中關(guān)于v-for循環(huán)key值問(wèn)題的研究
- vue中v-for循環(huán)數(shù)組,在方法中splice刪除數(shù)組元素踩坑記錄
- vue v-for循環(huán)出來(lái)的數(shù)據(jù)動(dòng)態(tài)綁定值問(wèn)題
- vue中的for循環(huán)以及自定義指令解讀
- vue.js中for循環(huán)如何實(shí)現(xiàn)異步方法同步執(zhí)行
相關(guān)文章
vue動(dòng)態(tài)組件實(shí)現(xiàn)選項(xiàng)卡切換效果
這篇文章主要為大家詳細(xì)介紹了vue動(dòng)態(tài)組件實(shí)現(xiàn)選項(xiàng)卡切換效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
vue微信分享出來(lái)的鏈接點(diǎn)開(kāi)是首頁(yè)問(wèn)題的解決方法
這篇文章主要為大家詳細(xì)介紹了vue微信分享出來(lái)的鏈接點(diǎn)開(kāi)是首頁(yè)問(wèn)題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
vue遞歸生成樹(shù)狀結(jié)構(gòu)的示例代碼
這篇文章主要介紹了vue遞歸生成樹(shù)狀結(jié)構(gòu)的示例,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-07-07
去除element-ui下拉框的下拉箭頭的實(shí)現(xiàn)
我們最開(kāi)始拿到的element-ui是帶有下拉箭頭的,那么如何去除element-ui下拉框的下拉箭頭的實(shí)現(xiàn),本文就詳細(xì)的介紹一下,感興趣的可以了解一下2023-08-08
vue-cli3項(xiàng)目打包后自動(dòng)化部署到服務(wù)器的方法
這篇文章主要介紹了vue-cli3項(xiàng)目打包后自動(dòng)化部署到服務(wù)器的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
關(guān)于vuex強(qiáng)刷數(shù)據(jù)丟失問(wèn)題解析
這篇文章主要介紹了關(guān)于vuex強(qiáng)刷數(shù)據(jù)丟失問(wèn)題解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
vue.js實(shí)現(xiàn)左邊導(dǎo)航切換右邊內(nèi)容
這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)左邊導(dǎo)航切換右邊內(nèi)容,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
element滾動(dòng)條組件el-scrollbar的使用詳解
本文主要介紹了element滾動(dòng)條組件el-scrollbar的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Vue?element-ui?el-cascader?只能末級(jí)多選問(wèn)題
這篇文章主要介紹了Vue?element-ui?el-cascader?只能末級(jí)多選問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

