vue?循環(huán)動(dòng)態(tài)設(shè)置ref并獲取$refs方式
vue循環(huán)動(dòng)態(tài)設(shè)置ref并獲取$refs
關(guān)于ref的使用和場(chǎng)景請(qǐng)看官網(wǎng)文檔
下面是我對(duì)循環(huán)設(shè)置ref并獲取使用的一些辦法,簡單記錄一下
一. 使用理解步驟(個(gè)人理解、大白話概況)
單循或雙循環(huán)標(biāo)簽使用,無論那種都要**保證ref唯一**
例如:可以在ref值后面附帶id或不會(huì)重復(fù)的索引
針對(duì)上述唯一的ref進(jìn)行獲取
獲?。簡窝h(huán)可以直接用$refs獲取非單循環(huán)可以通過eval()獲取
二. 單循環(huán)和無循環(huán)動(dòng)態(tài)設(shè)置ref
設(shè)置 【ref=“xxx”】【ref="‘xxx’+index"】
就不多說了 簡單
三. 雙循環(huán)動(dòng)態(tài)設(shè)置ref
設(shè)置【:ref="‘xxx’+id"】或【:ref="‘xxx’+index"】
<div v-for="(item,index) in topicList" :key="index"> ? ? ? <el-carousel-item v-for="(son,i) in item.questionList" :key="index+i"> ? ? ? ? ? ?<topic :ref="'topicRef'+son.bId"></topic> ? ? ? ? ? ?//或也可以用索引. ?用一個(gè)索引會(huì)重復(fù),如下 ? ? ? ? ? ?//<topic :ref="'topicRef'+(i+index)"></topic> ? ? ? </el-carousel-item> </div>
獲取
eval("that.$refs.tagItem" +(x+index))[0]或
eval("that.$refs.topicRef" +(ele.bId))[0]ref和$refs的使用方法
在JavaScript中需要通過document.querySelector("#demo")來獲取dom節(jié)點(diǎn),然后再獲取這個(gè)節(jié)點(diǎn)的值。
在Vue中,我們不用獲取dom節(jié)點(diǎn),元素綁定ref之后,直接通過this.$refs即可調(diào)用,這樣可以減少獲取dom節(jié)點(diǎn)的消耗。
ref介紹
ref被用來給元素或子組件注冊(cè)引用信息。引用信息將會(huì)注冊(cè)在父組件的 $refs對(duì)象上。如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;如果用在子組件上,引用就指向該子組件實(shí)例
通俗的講,ref特性就是為元素或子組件賦予一個(gè)ID引用,通過this.$refs.refName來訪問元素或子組件的實(shí)例
<p ref="p">Hello</p> <children ref="children"></children>
this.$refs.p this.$refs.children
this.$refs介紹
this.$refs是一個(gè)對(duì)象,持有當(dāng)前組件中注冊(cè)過 ref特性的所有 DOM 元素和子組件實(shí)例
注意: $refs只有在組件渲染完成后才填充,在初始渲染的時(shí)候不能訪問它們,并且它是非響應(yīng)式的,因此不能用它在模板中做數(shù)據(jù)綁定
案例一、ref 寫在標(biāo)簽上時(shí)
<!-- ref 寫在標(biāo)簽上時(shí):this.$refs.名字 獲取的是標(biāo)簽對(duì)應(yīng)的dom元素
ref 寫在組件上時(shí):這時(shí)候獲取到的是 子組件(比如counter)的引用-->
<div id="root">
<!-- ref = 'hello': 給div 起了一個(gè)引用的名字 hello -->
<div
ref = 'hello'
@click = "handleClick">
laugh yourself
</div>
</div>
<script>
var vm = new Vue({
el: '#root',
methods: {
handleClick: function() {
//this.$refs : 獲取整個(gè)Vue實(shí)例中所有的引用 然后再找到hello這個(gè)引用 指向div那個(gè)dom節(jié)點(diǎn)
//console.log(this.$refs.hello)
alert(this.$refs.hello.innerHTML)
}
}
})
</script>
案例二、 ref 寫在組件上時(shí)
計(jì)數(shù):
<!-- ref 寫在標(biāo)簽上時(shí):this.$refs.名字 獲取的是標(biāo)簽對(duì)應(yīng)的dom元素
ref 寫在組件上時(shí):這時(shí)候獲取到的是 子組件(比如counter)的引用-->
<div id="root">
<!-- 子組件觸發(fā)了事件 這里父組件(模板區(qū)里面)監(jiān)聽該事件 調(diào)用handleChange方法
因此handleChange方法定義在父組件的methods里面-->
<counter ref="one" @change="handleChange"></counter>
<counter ref="two" @change="handleChange"></counter>
<div>{{total}}</div>
</div>
<script>
Vue.component('counter', {
template: '<div @click="handleClick"> {{number}} </div>',
data: function() {
return {
number: 0
}
},
methods: {
handleClick: function() {
this.number ++
//子組件向父組件傳值 子組件被點(diǎn)擊的時(shí)候 number+1 同時(shí)告訴外面 也即是觸發(fā)一個(gè)事件
this.$emit('change')
}
},
})
var vm = new Vue({
el: '#root',
data: {
total: 0
},
methods: {
handleChange: function() {
//在此方法中計(jì)算兩個(gè)數(shù)量的和 通過this.$refs.引用名字 獲取兩個(gè)子組件的引用
this.total = this.$refs.one.number +
this.$refs.two.number
}
}
})
</script>
注意:
當(dāng)ref和v-for一起使用時(shí),獲取到的引用將會(huì)是一個(gè)數(shù)組,包含循環(huán)數(shù)組源
<template>
<div>
<div ref="myDiv" v-for="(item, index) in arr" :key="index">{{item}}</div>
</div>
</template>
<script>
export default {
data() {
return {
arr: ['one', 'two', 'three', 'four']
}
},
mounted() {
console.log(this.$refs.myDiv)
},
methods: {}
}
</script>
<style lang="sass" scoped>
</style>
實(shí)例(通過ref特性調(diào)用子組件的方法)
【1】子組件code:
<template>
<div>{{msg}}</div>
</template>
<script>
export default {
data() {
return {
msg: '我是子組件'
}
},
methods: {
changeMsg() {
this.msg = '變身'
}
}
}
</script>
<style lang="sass" scoped></style>【2】父組件code:
<template>
<div @click="parentMethod">
<children ref="children"></children>
</div>
</template>
<script>
import children from 'components/children.vue'
export default {
components: {
children
},
data() {
return {}
},
methods: {
parentMethod() {
this.$refs.children //返回一個(gè)對(duì)象
this.$refs.children.changeMsg() // 調(diào)用children的changeMsg方法
}
}
}
</script>
<style lang="sass" scoped></style>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中的Token過期驗(yàn)證與動(dòng)態(tài)路由重定向詳解
這篇文章主要為大家詳細(xì)介紹了如何在 Vue 項(xiàng)目中實(shí)現(xiàn) Token 過期驗(yàn)證,并根據(jù) Token 的有效期動(dòng)態(tài)重定向用戶到首頁或登錄頁,感興趣的小伙伴可以了解下2025-03-03
Vue3中關(guān)于getCurrentInstance的大坑及解決
這篇文章主要介紹了Vue3中關(guān)于getCurrentInstance的大坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Vue.js實(shí)現(xiàn)簡單計(jì)時(shí)器應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Vue.js實(shí)現(xiàn)簡單計(jì)時(shí)器應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Vue仿微信app頁面跳轉(zhuǎn)動(dòng)畫效果
這篇文章主要介紹了Vue仿微信app頁面跳轉(zhuǎn)動(dòng)畫效果,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
Vue打包部署到Nginx時(shí),css樣式不生效的解決方式
這篇文章主要介紹了Vue打包部署到Nginx時(shí),css樣式不生效的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
可能是全網(wǎng)vue?v-model最詳細(xì)講解教程
Vue官網(wǎng)教程上關(guān)于v-model的講解不是十分的詳細(xì),寫這篇文章的目的就是詳細(xì)的剖析一下,下面這篇文章主要給大家介紹了關(guān)于vue?v-model最詳細(xì)講解的相關(guān)資料,需要的朋友可以參考下2022-11-11
vue項(xiàng)目優(yōu)化之通過keep-alive數(shù)據(jù)緩存的方法
本篇文章主要介紹了vue項(xiàng)目優(yōu)化之通過keep-alive數(shù)據(jù)緩存的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
在vue項(xiàng)目中設(shè)置一些全局的公共樣式
這篇文章主要介紹了在vue項(xiàng)目中設(shè)置一些全局的公共樣式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

