vue获取input框的值
[toc]
一、问题
使用
this.$refs.getValue.value提示undefined
解决办法:
using v-show instead of v-if.
<div v-if="!isLoading">
<GMap ref="mapRef" />
</div>just changed that to v-show
<div v-show="!isLoading">
<GMap ref="mapRef" />
</div>原因:
And now the object is available in mounted(). Still find it strange that the console.log(this.$refs) showed it being available as a key on this.$refs, even though it actually wasn't? Thats strange behaviour.
The other wierd thing was, that even if I tried to access this.$refs.mapRef in my data loading section, AFTER THE DATA WAS LOADED, (ie after isLoading=false), I still couldn't access it. Even though then, it should've been available because the v-if passed.
So v-show solved it, by just hiding the div, instead of not rendereding it. Stupid little workaround.
二、进阶
vue 官网中ref 下有一段话 "关于 ref 注册时间的重要说明:因为 ref 本身是作为渲染结果被创建的,在初始渲染的时候你不能访问它们 - 它们还不存在!`$refs` 也不是响应式的,因此你不应该试图用它在模板中做数据绑定。"方法一:
解决办法:
1、如果你在mounted里获取this.$refs,因为dom还未完全加载,所以你是拿不到的, update阶段则是完成了数据更新到 DOM 的阶段(对加载回来的数据进行处理),此时,就可以使用this.$refs了
2、如果写在method中,那么可以使用 this.$nextTick(() => {}) 等页面渲染好再调用,这样就可以了
3、或者加个定时器延时加载this.$refs(这个方法还没有试)方法二:
<input type="text" @input="inputFn" />
//inputFn为函数,实时获取输入的值
//script
inputFn(e){
console.log(e)
}获取最后一次输入内容(防抖):
template
<el-input
type="text"
id = "customInput"
tabindex="1"
class = "on-startup-popup-input"
placeholder="Custom"
ref="getValue"
@input="customBtn"
></el-input>
</el-col>
<el-col :span="2"><div class="grid-content bg-purple-light" />
<el-button :style="customStyle" :disabled = "!customStyle.disabled" tabindex="1" @click="customURL" id = "customCheck" :type="customStyle.type" icon = "el-icon-check" circle/>
</el-col>script
changeInputStyle(args){
let res = util.isValidUrl(args)
this.customStyle.type = res?"primary":"info"
this.customStyle.disabled = res
this.customStyle.plain = res
this.customStyle.opacity = res? "1": "0.5"
},
customBtn(e){
let exe = this.debounceInput(this.changeInputStyle(e), 500)
exe(e)
},
debounceInput(callback, delay){
let timer= null
return function(args) {
if (timer) {clearTimeout(timer)}
timer = setTimeout(function () {
return callback(args)
}, delay)
}
},三、参考文档
http://soiiy.com/Vue-js/15265.html
四、拓展知识
keydown:当用户按下键盘上的任意键时触发,如果按住不放的话,会重复触发此事件;
keypress:当用户按下键盘上的字符键时触发,如果按住不让的话,会重复触发此事件;
keyup:当用户释放键盘上的字符键时触发visible和display的区别
visibility是用来设置元素是隐藏的还是显示的,visible就表示显示,而hidden就表示隐藏,当我们把visibility属性设置成hidden,表示该元素是隐藏的,但是隐藏的位置还是保留的,其实的文字不会移动。
visibility: visible
/* 元素可见,默认值*/
visibility: hidden
/* 元素不可见,但仍然为其保留相应的空间*/
visibility: collapse
/* 只对 table 对象起作用,能移除行或列但不会影响表格的布局。如果这个值用在 table 以外的对象上则表现为 hidden 。*/
visibility: inherit
/* 继承上级元素的 visibility 值。*/display是隐藏之后,对浮动的一种设置。
display: none
/* 元素不可见,并且不为其保留相应的位置*/
display: block
/* 表现为一个块级元素(一般情况下独占一行)*/
display: inline
/* 表现为一个行级元素(一般情况下不独占一行)*/1.用法不同
visibility的用法是有四个值,经常用到的就是隐藏和显示,而display 属性的值有很多,但是我们只关注几个值,block、none 和 inline。
2.隐藏的空间
visibility在原空间会保留, display中的none会把元素从页面上删除,并且在页面上看不到元素是否存在。
如果你想隐藏元素,并且要求在页面上保留元素空间的话,就可以使用visibility: hidden,如果你想隐藏并且改变布局的话就使用 display: none。
