vue $ref的基本用法
2022/1/9 6:07:14
本文主要是介绍vue $ref的基本用法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、使用在一般的标签上
<div id="app"> <input ref="count" type="text" v-model="active.name" required name="name" value=""> </div>
这样在vue中我们可以使用$ref来获取dom节点,进行一些dom的操作
下面示例:控制input输入框的文字个数
new Vue({ el:'#app', data:{ active:{'name':''} }, watch:{ active:{ handler:function(){ var _this = this; var _sum = 4; //字数限制为4个 _this.$refs.count.setAttribute("maxlength",_sum); }, deep:true } }, })
2、使用在子组件上,可以用来获取子组件的属性值,假设子组件里面都有一个属性news
<div id="app"> <hdnews ref="hdnews"></hdnews> <hdinfo ref="hdinfo"></hdinfo> </div>
new Vue({ el:'#app', mounted:function () { console.log(this.$refs.hdnews.news); console.log(this.$refs.hdinfo.news); } })
转 : https://blog.csdn.net/ljc20090913/article/details/81662690
这篇关于vue $ref的基本用法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-27使用js将ETH账户的资产打散其他账户web3
- 2024-09-27我轻松地将我的 React.js 应用程序翻译成了多种语言。下面是我是如何做到的... ??
- 2024-09-27?? 使用 useMemo 和 useCallback 加速 React:告别缓慢的重新渲染!??
- 2024-09-27Vue CLI多环境配置教程:新手入门指南
- 2024-09-27Vue CLI多环境配置教程:快速入门指南
- 2024-09-27Vue CLI教程:新手入门指南
- 2024-09-27Vue CLI教程:初学者快速入门指南
- 2024-09-27Vue3公共组件教程:入门与实践
- 2024-09-27Vue3公共组件教程:新手入门指南
- 2024-09-27Vue3教程:初学者快速入门指南