Vue 中$els 的用法和理解

来源:互联网 发布:淘宝助理5.5官方版 编辑:程序博客网 时间:2024/06/05 07:08

最近开发项目中用到了$els 经过查阅后来归纳一下


$els 为 DOM 元素注册一个索引,方便通过所属实例的 $els 访问这个元素。

注意:

因为 HTML 不区分大小写,camelCase 名字比如 v-el:someEl 将转为全小写。可以用 v-el:some-el 设置 this.$els.someEl。

我的理解:$els类似于document.querySelector('.class')的功能,可以拿到指定的dom元素。(注意v-el在vue2.0中已经删除改为ref="someEl" 驼峰形式的命名)

Eg:

<div v-el:mainContainer></div>

var _dom = this.$els.maincontainer

注意:在取该dom元素时,不识别驼峰命名,如上例,绑定的值为mainContainer,但是在取dom的时候只能写maincontainer,否则将无法识别.

 _initScroll(){     this.meunScroll=new BScroll(this.$els.goodsWarpper)     }

 <div class="goods_warpper mui-scroll-wrapper" style="left: 0;" ref="goodsWarpper">