vue基础(五):给DOM添加事件的特殊情况 $nextTick

$ nextTick是在DOM更新循环结束之后执行的延迟回调,在修改数据之后使用$nextTick可在回调中获取到更新后的DOM。

new Vue({
	 el: '#app',
	 data() {
	 	return{
	 		isShow:false
	 	}
	},
	 template:'
'
mounted: function() { this.isShow=true; console.log(this.$refs.input) //this.$refs.input.focus() ;此时并无法获取焦点 this.$nextTick(function () {//获取更新的DOM再聚焦 this.$refs.input.focus() }) }, });

你可能感兴趣的:(vue.js)