Vue2中ref属性的使用方法

一.了解ref

ref 被用来给元素或子组件注册引用信息。引用信息将会注册在父组件的 $refs 对象上。

1.如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;

<template>
    <div>
        <input type="text" ref="inp" id="inp"/>
        <button @click="add">添加</button>
    </div>
</template>
<script>
export default {
	methods:{
		add:function(){
			this.$refs.inp.value='22';
			console.log(this.$refs.inp);
		}
	}
}
</script>

从上面的列子可以看出,inp的引用信息为inp ,$refs 是所有注册过的ref的一个集合

console.log(this.$refs.inp)//<input type="text" id="inp">
console.log(document.getElementById('inp'))//<input type="text" id="inp">
 这两种方法获得的都是Dom节点,而$refs相对document.getElementById的方法,会减少获取dom节点的消耗

2.如果用在子组件上,引用就指向组件实例:

尽管有 prop 和事件,但是有时仍然需要在 JavaScript 中直接访问子组件。为此可以使用 ref 为子组件指定一个引用 ID。例如:

<template>
    <div>
        <input type="text" ref="inp" id="inp"/>
        <button @click="add">添加</button>
        <child ref="childs"></child>
    </div>
</template>
<script>
import Vue from 'vue';  
Vue.component('child',{
	template:"<div>我是一个child组件! </div>"
})
export default {
	methods:{
		add:function(){
			this.$refs.inp.value='22';
			console.log(this.$refs.inp);
			console.log(this.$refs.childs);
		}
	}
}
</script>

3.与for连用的时候

当 ref 和 v-for 一起使用时,获取到的引用会是一个数组,包含和循环数据源对应的子组件。

<template>
    <div>
        <input type="text" ref="inp" id="inp"/>
        <button @click="add">添加</button>
        <child ref="childs"></child>
        <ul v-for="item in list">
        	<li  ref="refContent">姓名:{{item.name}},年龄:{{item.age}}</li>
        </ul>
    </div>
</template>
<script>
import Vue from 'vue';  
Vue.component('child',{
	template:"<div>我是一个child组件! </div>"
})
export default {
	data() {
		return{
			list:[{name:'小高',age:18},{name:'小吴',age:20},{name:'小陈',age:23}]
		}
    },
    mounted() {
		console.log(this.$refs.refContent);
    },
	methods:{
		add:function(){
			this.$refs.inp.value='22';
			console.log(this.$refs.inp);
			
		}
	}
}
</script>


  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 ,`ref` 是一个新的响应式 API,用于在模板引用一个元素或组件,并可以在 JavaScript 代码访问该元素或组件的属性方法。 在模板,可以使用 `ref` 指令来创建一个 `ref` 对象,并将其绑定到一个元素或组件上。例如: ```html <template> <div> <input type="text" ref="inputRef" /> <button @click="handleClick">Click Me</button> </div> </template> <script> import { ref } from 'vue'; export default { setup() { const inputRef = ref(null); function handleClick() { inputRef.value.focus(); } return { inputRef, handleClick, }; }, }; </script> ``` 在这个例子,我们使用 `ref` 指令将 `input` 元素绑定到 `inputRef` 变量上。在 `setup` 函数,我们使用 `ref` 函数创建了一个 `ref` 对象,并将其初始值设置为 `null`。然后,我们在 `handleClick` 函数访问了 `inputRef.value`,并调用了 `focus` 方法,以便将焦点设置到 `input` 元素上。 需要注意的是,在 Vue 3 ,`ref` 不再返回一个对象,而是返回一个包含 `value` 属性的普通 JavaScript 对象。因此,在访问 `ref` 对象的属性方法时,需要使用 `.value` 来访问其值。 另外,在 Vue 3 ,`ref` 还可以用于引用组件,例如: ```html <template> <div> <MyComponent ref="myComponentRef" /> <button @click="handleClick">Click Me</button> </div> </template> <script> import { ref } from 'vue'; import MyComponent from './MyComponent.vue'; export default { components: { MyComponent, }, setup() { const myComponentRef = ref(null); function handleClick() { myComponentRef.value.someMethod(); } return { myComponentRef, handleClick, }; }, }; </script> ``` 在这个例子,我们使用 `ref` 指令将 `MyComponent` 组件绑定到 `myComponentRef` 变量上。在 `setup` 函数,我们使用 `ref` 函数创建了一个 `ref` 对象,并将其初始值设置为 `null`。然后,我们在 `handleClick` 函数访问了 `myComponentRef.value`,并调用了 `someMethod` 方法,以便调用 `MyComponent` 组件的某个方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值