色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Vue 實(shí)例中使用$refs的注意事項(xiàng)

瀏覽:8日期:2022-10-07 14:43:26

在開(kāi)發(fā)過(guò)程中,經(jīng)常會(huì)通過(guò)實(shí)例的vm.$refs(this.$refs)取得通過(guò)ref注冊(cè)過(guò)的組件,并進(jìn)行相應(yīng)操作,但存在取不到元素的情況,其根本原因是因?yàn)?refs只能取得mounted(渲染)之后的元素。

Vue 實(shí)例中使用$refs的注意事項(xiàng)

例如,在這種情況中,若flag從真值切換到假值取不到節(jié)點(diǎn)是正常的,因?yàn)関-if如果為假值,那么該節(jié)點(diǎn)不會(huì)被渲染。

但如果從假值切換到真值時(shí),也可能取不到節(jié)點(diǎn),這是因?yàn)殇秩拘枰獣r(shí)間,通常可以使用$nextTick()解決。

...<el-table v-if='flag' ref='table'> <el-table-column prop='prop1'></el-table-column> <el-table-column prop='prop2'></el-table-column></el-table>... export default { methods: { this.$refs.table.XXX() }}

但存在一個(gè)極特殊的情況,第一次頁(yè)面渲染的時(shí)候,$refs也取不到值。這個(gè)時(shí)候就要考慮v-show進(jìn)行組件元素的隱藏與展示。

因?yàn)関-show是通過(guò)css的display:none進(jìn)行隱藏控制,所以一開(kāi)始就會(huì)渲染,肯定能夠取到元素

補(bǔ)充:Vue.js中ref ($refs)用法舉例總結(jié)及應(yīng)注意的坑

一、根據(jù)官方文檔總結(jié)的用法:

看Vue.js文檔中的ref部分,自己總結(jié)了下ref的使用方法以便后面查閱。

1、ref使用在外面的組件上

HTML 部分

<div v-on:click='consoleRef'> <component-father ref='outsideComponentRef'> </component-father> <p>ref在外面的組件上</p></div>

js部分

var refoutsidecomponentTem={ template:'<div class=’childComp’><h5>我是子組件</h5></div>' }; var refoutsidecomponent=new Vue({ el:'#ref-outside-component', components:{ 'component-father':refoutsidecomponentTem }, methods:{ consoleRef:function () {console.log(this); // #ref-outside-component vue實(shí)例console.log(this.$refs.outsideComponentRef); // div.childComp vue實(shí)例 } } });

2、ref使用在外面的元素上

HTML部分

<!--ref在外面的元素上--><div v-on:click='consoleRef' > <component-father> </component-father> <p ref='outsideDomRef'>ref在外面的元素上</p></div>

JS部分

var refoutsidedomTem={ template:'<div class=’childComp’><h5>我是子組件</h5></div>' }; var refoutsidedom=new Vue({ el:'#ref-outside-dom', components:{ 'component-father':refoutsidedomTem }, methods:{ consoleRef:function () {console.log(this); // #ref-outside-dom vue實(shí)例console.log(this.$refs.outsideDomRef); // <p> ref在外面的元素上</p> } } });

3、ref使用在里面的元素上---局部注冊(cè)組件

HTML部分

<!--ref在里面的元素上--><div id='ref-inside-dom'> <component-father> </component-father> <p>ref在里面的元素上</p></div>

JS部分

var refinsidedomTem={ template:'<div class=’childComp’ v-on:click=’consoleRef’>' + '<h5 ref=’insideDomRef’>我是子組件</h5>' + '</div>', methods:{ consoleRef:function () {console.log(this); // div.childComp vue實(shí)例 console.log(this.$refs.insideDomRef); // <h5 >我是子組件</h5> } } }; var refinsidedom=new Vue({ el:'#ref-inside-dom', components:{ 'component-father':refinsidedomTem } });

4、ref使用在里面的元素上---全局注冊(cè)組件

HTML部分

<!--ref在里面的元素上--全局注冊(cè)--><div id='ref-inside-dom-all'> <ref-inside-dom-quanjv></ref-inside-dom-quanjv></div>

JS部分

Vue.component('ref-inside-dom-quanjv',{ template:'<div class=’insideFather’> ' + '<input type=’text’ ref=’insideDomRefAll’ v-on:input=’showinsideDomRef’>' + ' <p>ref在里面的元素上--全局注冊(cè) </p> ' + '</div>', methods:{ showinsideDomRef:function () {console.log(this); //這里的this其實(shí)還是div.insideFatherconsole.log(this.$refs.insideDomRefAll); // <input type='text'> } } }); var refinsidedomall=new Vue({ el:'#ref-inside-dom-all' });二、應(yīng)注意的坑

1、如果通過(guò)v-for 遍歷想加不同的ref時(shí)記得加 :號(hào),即 :ref =某變量 ;

這點(diǎn)和其他屬性一樣,如果是固定值就不需要加 :號(hào),如果是變量記得加 :號(hào)

Vue 實(shí)例中使用$refs的注意事項(xiàng)

2、通過(guò) :ref =某變量 添加ref(即加了:號(hào)) ,如果想獲取該ref時(shí)需要加 [0],如this.$refs[refsArrayItem] [0];如果不是:ref =某變量的方式而是 ref =某字符串時(shí)則不需要加,如this.$refs[refsArrayItem]

Vue 實(shí)例中使用$refs的注意事項(xiàng)

加和不加[0]的區(qū)別--未展開(kāi)

Vue 實(shí)例中使用$refs的注意事項(xiàng)

加和不加[0]的區(qū)別--展開(kāi)了

3、想在element ui 對(duì)話框打開(kāi)后取dom時(shí),應(yīng)該使用$nextTick,而不是直接使用this.$refs. imgLocal2:

console.log(’this.$refs.imgLocal2外面’, this.$refs.imgLocal2); setTimeout(() => { console.log(’this.$refs.imgLocal2 setTimeout’, this.$refs.imgLocal2); }, 500); // 不推薦 this.$nextTick(() => { console.log(’this.$refs.imgLocal2 $nextTick’, this.$refs.imgLocal2); });

Vue 實(shí)例中使用$refs的注意事項(xiàng)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 久色乳综合思思在线视频 | 久久九九免费 | 68久久久久欧美精品观看 | 国产精品人成人免费国产 | 亚洲一区 中文字幕 久久 | 精品国产高清毛片 | 国产精品久久大陆 | 亚洲欧美日韩国产专区一区 | 国产一极毛片 | 久久久久久一品道精品免费看 | 国产精品玖玖 | 久久国产视频网站 | 综合色久 | 亚洲国产精久久久久久久 | 午夜精品久视频在线观看 | 在线观看精品国内福利视频 | 国产综合在线播放 | 亚洲欧美卡通动漫丝袜美腿 | 精品午夜久久网成年网 | 国产男女免费视频 | 亚洲第一视频在线播放 | 女性无套免费网站在线看 | 日本在线免费播放 | 9丨精品国产高清自在线看 ⅹxx中国xxx人妖 | 看欧美毛片一级毛片 | 精品国产日韩亚洲一区二区 | 日韩加勒比 | 久久久久亚洲视频 | 久久a热6 | 久久欧美久久欧美精品 | a毛片免费观看完整 | 亚洲爽爽 | 欧美一级精品 | 国产激情久久久久久影院 | 在线视频观看免费视频18 | 91亚洲精品一区二区在线观看 | 国产麻豆交换夫妇 | 美女在线网站免费的 | 免费aⅴ在线 | 亚洲偷自拍另类图片二区 | 国产一区二区三区成人久久片 |