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

您的位置:首頁技術文章
文章詳情頁

Vue 實例中使用$refs的注意事項

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

在開發過程中,經常會通過實例的vm.$refs(this.$refs)取得通過ref注冊過的組件,并進行相應操作,但存在取不到元素的情況,其根本原因是因為$refs只能取得mounted(渲染)之后的元素。

Vue 實例中使用$refs的注意事項

例如,在這種情況中,若flag從真值切換到假值取不到節點是正常的,因為v-if如果為假值,那么該節點不會被渲染。

但如果從假值切換到真值時,也可能取不到節點,這是因為渲染需要時間,通常可以使用$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() }}

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

因為v-show是通過css的display:none進行隱藏控制,所以一開始就會渲染,肯定能夠取到元素

補充:Vue.js中ref ($refs)用法舉例總結及應注意的坑

一、根據官方文檔總結的用法:

看Vue.js文檔中的ref部分,自己總結了下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實例console.log(this.$refs.outsideComponentRef); // div.childComp vue實例 } } });

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實例console.log(this.$refs.outsideDomRef); // <p> ref在外面的元素上</p> } } });

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

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實例 console.log(this.$refs.insideDomRef); // <h5 >我是子組件</h5> } } }; var refinsidedom=new Vue({ el:'#ref-inside-dom', components:{ 'component-father':refinsidedomTem } });

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

HTML部分

<!--ref在里面的元素上--全局注冊--><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在里面的元素上--全局注冊 </p> ' + '</div>', methods:{ showinsideDomRef:function () {console.log(this); //這里的this其實還是div.insideFatherconsole.log(this.$refs.insideDomRefAll); // <input type='text'> } } }); var refinsidedomall=new Vue({ el:'#ref-inside-dom-all' });二、應注意的坑

1、如果通過v-for 遍歷想加不同的ref時記得加 :號,即 :ref =某變量 ;

這點和其他屬性一樣,如果是固定值就不需要加 :號,如果是變量記得加 :號

Vue 實例中使用$refs的注意事項

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

Vue 實例中使用$refs的注意事項

加和不加[0]的區別--未展開

Vue 實例中使用$refs的注意事項

加和不加[0]的區別--展開了

3、想在element ui 對話框打開后取dom時,應該使用$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 實例中使用$refs的注意事項

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Vue
相關文章:
主站蜘蛛池模板: 91热视频在线观看 | 成人免费视频在线看 | 99久久精品国产免费 | 欧美一区二区三区男人的天堂 | 日韩三级视频 | 无圣光福利视频 | 国产成人精品久久 | 久久久99精品免费观看精品 | 中午字幕在线观看 | 又刺激又黄的一级毛片 | 免费观看一级欧美大 | 成人亚洲网| 泰国情欲片寂寞的寡妇在线观看 | 高清色黄毛片一级毛片 | 欧美一级毛片生活片 | 精品久久久久久久久免费影院 | 一区二区三区网站在线免费线观看 | 久久这里只有精品免费视频 | 精品国产综合区久久久久99 | 在线看片亚洲 | 香港激情三级做爰小说 | 国内精品国语自产拍在线观看55 | 欧美日本一二三区 | 日韩a级毛片免费视频 | 国产亚洲精品成人久久网站 | 成人三级在线播放 | 国产精品一级毛片不收费 | 久久免费视频网站 | 欧美一级鲁丝片免费看 | 伊人久热这里只有精品视频99 | 国产丝袜美女一区二区三区 | 国内精品99 | 久久精品亚洲一级毛片 | 欧美成人猛男性色生活 | 欧美成人性色xxxx视频 | 国产大秀视频 | 久热精品男人的天堂在线视频 | 日本一级级特黄特色大片 | 亚洲欧美日本韩国综合在线观看 | 欧美做爰性欧美 | 国产精品久久久久国产精品 |