vue里ref ($refs)用法
Vue 用法 ref Refs
2023-09-11 14:19:54 时间
ref 有三种用法:
1、ref 加在普通的元素上,用this.ref.name 获取到的是dom元素
2、ref 加在子组件上,用this.ref.name 获取到的是组件实例,可以使用组件的所有方法。
3、如何利用 v-for 和 ref 获取一组数组或者dom 节点
注意:
1、ref 需要在dom渲染完成后才会有,在使用的时候确保dom已经渲染完成。比如在生命周期 mounted(){} 钩子中调用,或者在 this.$nextTick(()=>{}) 中调用。
2、如果ref 是循环出来的,有多个重名,那么ref的值会是一个数组 ,此时要拿到单个的ref 只需要循环就可以了。
一、ref使用在外面的组件上
<div id="ref-outside-component" v-on:click="consoleRef">
<component-father ref="outsideComponentRef">
</component-father>
<p>ref在外面的组件上</p>
</div>
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实例,组件实例
}
}
});
二、ref作用在外面元素上
//ref在外面的元素上
<div id="ref-outside-dom" v-on:click="consoleRef" >
<component-father>
</component-father>
<p ref="outsideDomRef">ref在外面的元素上</p>
</div>
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>标签dom元素 ref在外面的元素上</p>
}
}
});
三、ref使用在里面的元素上--局部注册组件
//ref在里面的元素上
<div id="ref-inside-dom">
<component-father>
</component-father>
<p>ref在里面的元素上</p>
</div>
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
}
});
四、ref使用在里面的元素上--全局注册组件
//ref在里面的元素上--全局注册
<div id="ref-inside-dom-all">
<ref-inside-dom-quanjv></ref-inside-dom-quanjv>
</div>
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.insideFather
console.log(this.$refs.insideDomRefAll); // <input type="text">
}
}
});
var refinsidedomall=new Vue({
el:"#ref-inside-dom-all"
});
相关文章
- vue.js响应式原理解析与实现
- Vue -> 解决 vue-ueditor-wrap 不能显示的问题
- VUE中使用ElementUi的Message弹窗提示
- [Nuxt] Display Vuex Data Differently in Each Page of Nuxt and Vue.js
- [Vue] Conditionally Render DOM Elements in Vue.js (v-if v-else v-show)
- vue和微信小程序的区别、比较
- Vue.js3: 页面打开前实现图片全部预加载(vue@3.2.33)
- vue框架-基础1-vue包引入,指令,模板语言
- [Vue @Component] Simplify Vue Components with vue-class-component
- vue.js3: 用pdf-lib给pdf文件添加中文字体水印(pdf-lib@1.17.1 / vue@3.2.37)
- Vue.js3: 页面打开前实现图片全部预加载(vue@3.2.33)
- vue-cli工程目录结构及相关文件说明
- 一个典型的Vue应用的App.vue
- 移动端h5网页、微信网页调试之利用vConsole真机调试+显示控制台打印信息、调试接口(附带vue项目里的具体使用方法)
- vue-router路由的安装、配置、具体用法和Vue-router学习笔记
- vue 点击切换动态修改Class变化,实现当前样式改变(点击选中高亮)
- vue里面$emit的用法
- vue脚手架基础demo1
- 计算机毕设 SSM Vue的汽车租赁系统(含源码+论文)
- Springboot+Vue实现将图片和表单一起提交到后端,同时将图片地址保存到数据库、再次将存储的图片展示到前端vue页面
- Vue中vue-i18n结合element-ui实现国际化
- 108:vue+openlayers实现多个图形的合并、交叉、差集等( 示例代码 )
- 104:vue+openlayers 配置Icon和text的参数( 代码示例 )
- Vue(八)vue 脚手架、脚手架创建项目示例
- 【vue】如何安装vue 脚手架以及创建脚手架项目_10
- vue+element ui+腾讯云开发打造会员管理系统之实战教程(六)类别管理
- 基于Java+SpringBoot+Vue前后端分离小区管理系统设计与实现
- Vue: vue-router路由
- 测试开发之Vue学习笔记-Vue基础