vue?循環(huán)動態(tài)設(shè)置ref并獲取$refs方式
vue循環(huán)動態(tài)設(shè)置ref并獲取$refs
關(guān)于ref的使用和場景請看官網(wǎng)文檔
下面是我對循環(huán)設(shè)置ref并獲取使用的一些辦法,簡單記錄一下
一. 使用理解步驟(個人理解、大白話概況)
單循或雙循環(huán)標簽使用,無論那種都要**保證ref唯一**
例如:可以在ref值后面附帶id或不會重復(fù)的索引
針對上述唯一的ref進行獲取
獲?。簡窝h(huán)可以直接用$refs獲取非單循環(huán)可以通過eval()獲取
二. 單循環(huán)和無循環(huán)動態(tài)設(shè)置ref
設(shè)置 【ref=“xxx”】【ref="‘xxx’+index"】
就不多說了 簡單
三. 雙循環(huán)動態(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> ? ? ? ? ? ?//或也可以用索引. ?用一個索引會重復(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é)點,然后再獲取這個節(jié)點的值。
在Vue中,我們不用獲取dom節(jié)點,元素綁定ref之后,直接通過this.$refs即可調(diào)用,這樣可以減少獲取dom節(jié)點的消耗。
ref介紹
ref被用來給元素或子組件注冊引用信息。引用信息將會注冊在父組件的 $refs對象上。如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;如果用在子組件上,引用就指向該子組件實例
通俗的講,ref特性就是為元素或子組件賦予一個ID引用,通過this.$refs.refName來訪問元素或子組件的實例
<p ref="p">Hello</p> <children ref="children"></children>
this.$refs.p this.$refs.children
this.$refs介紹
this.$refs是一個對象,持有當(dāng)前組件中注冊過 ref特性的所有 DOM 元素和子組件實例
注意: $refs只有在組件渲染完成后才填充,在初始渲染的時候不能訪問它們,并且它是非響應(yīng)式的,因此不能用它在模板中做數(shù)據(jù)綁定
案例一、ref 寫在標簽上時
<!-- ref 寫在標簽上時:this.$refs.名字 獲取的是標簽對應(yīng)的dom元素 ref 寫在組件上時:這時候獲取到的是 子組件(比如counter)的引用--> <div id="root"> <!-- ref = 'hello': 給div 起了一個引用的名字 hello --> <div ref = 'hello' @click = "handleClick"> laugh yourself </div> </div> <script> var vm = new Vue({ el: '#root', methods: { handleClick: function() { //this.$refs : 獲取整個Vue實例中所有的引用 然后再找到hello這個引用 指向div那個dom節(jié)點 //console.log(this.$refs.hello) alert(this.$refs.hello.innerHTML) } } }) </script>
案例二、 ref 寫在組件上時
計數(shù):
<!-- ref 寫在標簽上時:this.$refs.名字 獲取的是標簽對應(yīng)的dom元素 ref 寫在組件上時:這時候獲取到的是 子組件(比如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 ++ //子組件向父組件傳值 子組件被點擊的時候 number+1 同時告訴外面 也即是觸發(fā)一個事件 this.$emit('change') } }, }) var vm = new Vue({ el: '#root', data: { total: 0 }, methods: { handleChange: function() { //在此方法中計算兩個數(shù)量的和 通過this.$refs.引用名字 獲取兩個子組件的引用 this.total = this.$refs.one.number + this.$refs.two.number } } }) </script>
注意:
當(dāng)ref和v-for一起使用時,獲取到的引用將會是一個數(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>
實例(通過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 //返回一個對象 this.$refs.children.changeMsg() // 調(diào)用children的changeMsg方法 } } } </script> <style lang="sass" scoped></style>
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解element-ui中el-select的默認選擇項問題
這篇文章主要介紹了詳解element-ui中el-select的默認選擇項問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08vue+node實現(xiàn)圖片上傳及預(yù)覽的示例方法
這篇文章主要介紹了vue+node實現(xiàn)圖片上傳及預(yù)覽的示例方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11通過Element ui往頁面上加一個分頁導(dǎo)航條的方法
這篇文章主要介紹了通過Element ui往頁面上加一個分頁導(dǎo)航條的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05