$refs的用法及作用
获取DOM元素,一般用document.querySelector获取这个dom节点,然后在获取input的值
但是用ref绑定之后,就不需要在获取dom节点了,直接在上面的input上绑定input1,然后$refs里面调用即可
this.$refs.input1 这样可以减少获取DOM节点的消耗
<div id="app">
<input type="text" ref="input1"/>
<button @click="add">添加</button>
</div>
new Vue({
el: "#app",
methods:{
add:function(){
//this.$refs.input1 减少获取dom节点的消耗
this.$refs.input1.value ="996";
}
}
})
关于或且非的逻辑
或遇真则停,且遇假则停,否则返回最后一个值
或有一个真则真,且有一个价假则假
forEach 是 ES5 中操作数组的一种方法,主要功能是遍历数组,其实说穿了,就是 for 循环的加强版,该语句需要一个回调函数,作为参数。回调函数的形参,依次为,value:遍历的数组内容;index:对应的数组索引,array:数组本身。
// 分别对应:数组元素,元素的索引,数组本身
var arr = ['a','b','c'];
arr.forEach(function(value,index,array){
console.log(value);
console.log(index);
console.log(array);
})
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9

for 循环比较步骤多比较复杂,forEach 循环比较简单好用,不容易出错。
后续会继续补充。。。
本文介绍Vue中$refs的使用方法,通过绑定ref属性,可以直接访问DOM元素,提高性能。同时,对比了forEach与for循环的区别,强调了逻辑运算符的特性。

200

被折叠的 条评论
为什么被折叠?



