ref 的三种用法

ref 有三种用法:
  1、ref 加在普通的元素上,用this.$refs.(ref值) 获取到的是dom元素

2、ref 加在子组件上,用this. r e f s . ( r e f 值 ) 获 取 到 的 是 组 件 实 例 , 可 以 使 用 组 件 的 所 有 方 法 。 在 使 用 方 法 的 时 候 直 接 t h i s . refs.(ref值) 获取到的是组件实例,可以使用组件的所有方法。在使用方法的时候直接this. refs.ref使使this.refs.(ref值).方法() 就可以使用了。

3、如何利用 v-for 和 ref 获取一组数组或者dom 节点

应注意的坑:
1、如果通过v-for 遍历想加不同的ref时记得加 :号,即 :ref =某变量 ;
这点和其他属性一样,如果是固定值就不需要加 :号,如果是变量记得加 :号。(加冒号的,说明后面的是一个变量或者表达式;没加冒号的后面就是对应的字符串常量(String))

2、通过 :ref =某变量 添加ref(即加了:号) ,如果想获取该ref时需要加 [0],如this. r e f s [ r e f s A r r a y I t e m ] [ 0 ] ; 如 果 不 是 : r e f = 某 变 量 的 方 式 而 是   r e f = 某 字 符 串 时 则 不 需 要 加 , 如 t h i s . refs[refsArrayItem] [0];如果不是:ref =某变量的方式而是 ref =某字符串时则不需要加,如this. refs[refsArrayItem][0]:ref= ref=this.refs[refsArrayItem]。

1、ref 需要在dom渲染完成后才会有,在使用的时候确保dom已经渲染完成。比如在生命周期 mounted(){} 钩子中调用,或者在 this.$nextTick(()=>{}) 中调用。

2、如果ref 是循环出来的,有多个重名,那么ref的值会是一个数组 ,此时要拿到单个的ref 只需要循环就可以了。

例子1:
添加ref属性

这是H1

<button @click="getref">获取H1元素</button>
获取注册过 ref 的所有组件或元素 methods: { getref() { // 表示从 $refs对象 中, 获取 ref 属性值为: h1ele DOM元素或组件 console.log(this.$refs.h1Ele.innerText); this.$refs.h1ele.style.color = 'red';// 修改html样式
      console.log(this.$refs.ho.msg);// 获取组件数据
      console.log(this.$refs.ho.test);// 获取组件的方法
    }
  }

例子2:
Vue代码:

            <el-table @sort-change="sortChange" ref="multipleSelection" border :data="valueDryGoodTableData" style="width: 100%">
                <el-table-column align="left" prop="title" label="标题" min-width="80%" sortable="custom">
                    <template slot-scope="scope">
                        <a target="_blank" :class="scope.row.titleClicked?'titleClicked':''" class="hoverHand bluetext" v-html="scope.row.title" @click="titleClick(scope.row.articleUrl,scope.$index)">
                        </a>
                    </template>
                </el-table-column>
                <el-table-column align="left" prop="releaseTime" label="发布日期" min-width="11%" sortable="custom"></el-table-column>
                <el-table-column align="center" label="操作" min-width="9%">
                    <template slot-scope="scope">
                        <span class="operatoryTools">
                            <i title="取消收藏" v-if="scope.row.isFavour" @click="favoriteOperating(scope.row.id, scope.$index)" class="hoverHand iconStyle el-icon-star-on"></i>
                            <i title="收藏" v-else @click="favoriteOperating(scope.row.id, scope.$index)" class="hoverHand iconStyle el-icon-star-off"></i>
                            <i title="分享" @click.stop="showShareOperation(scope.$index)" class="shareTarg iconfont">&#xe678;</i>
                            <div class="share" v-if="scope.row.showShare">
                                <img class="hoverHand shareItem" title="分享到微博" @click="shareItem('sina',$event);" src="@/images/WEIBO.png">
                                <img class="hoverHand shareItem" title="分享到微信" @click.stop="shareItem('wx',$event);" src="@/images/WEIXIN.png">
                                <img class="hoverHand shareItem" title="分享到QQ" @click="shareItem('qq',$event);" src="@/images/QQ.png">
                            </div>
                            <div v-show="scope.row.erweimaShare" class="erweima_share"></div>
                            <div v-show="scope.row.erweimaShare1" class="erweima_share1"></div>
                        </span>
                    </template>
                </el-table-column>
            </el-table>

JS代码:

//点击清空条件,调用该方法
emptyAndSearch(){//清空条件方法
//清空树选中状态
this.clearTreeNodeCheck([‘tree’]);
//清除排序
this. r e f s . m u l t i p l e S e l e c t i o n . c l e a r S o r t ( ) ; / / 设 置 分 页 参 数 t h i s . q u e r y P a r a m = s t a r t R o w : 1 , p a g e S i z e : 20 , c o n d i t i o n : / / 分 页 查 询 调 用 t h i s . c o n f i r m S e a r c h ( ′ s t a t i s t i c s ′ ) ; / / 设 置 清 空 条 件 为 f a l s e t h i s . refs.multipleSelection.clearSort(); //设置分页参数 this.queryParam = { startRow : 1, pageSize : 20, condition:{ } } //分页查询调用 this.confirmSearch('statistics'); //设置清空条件为false this. refs.multipleSelection.clearSort();//this.queryParam=startRow:1,pageSize:20,condition://this.confirmSearch(statistics);//falsethis.store.commit(‘valueDryGoodDatas/SET_CLEAR_ALL’,false);
}
例子3:
Vue代码:






<el-button
type=“primary”
plain
size=“mini”
@click=“viewPcPic(queryFrom.picUrl)”>{{ $t(‘查看’) }}

JS代码:

  //获取元素清除验证
          this.$refs.goodPicInfoFormpicUrl.clearValidate()

一般来讲,想获取INPUT框,首先在获取DOM元素,需document.querySelector(".input1")获取这个dom节点,然后在获取input1的值。

但是用ref绑定之后,我们就不需要在获取dom节点了,直接在上面的input上绑定input1,然后$refs里面调用就行。

然后在javascript里面这样调用:this.$refs.input1  这样就可以减少获取dom节点的消耗了

————————————————
版权声明:本文为CSDN博主「小鸡会蹦迪」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wh710107079/article/details/88243638

  • 9
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值