vue优势:mvvm 在vue中,程序员不需要操作dom。只要把数据维护好就行。
ref用来辅助开发者在不依赖于jQuery的情况下,获取dom元素或组件引用。
在每个vue组件实例上都包含一个$refs对象
获取dom元素
<h1 ref="myh1">app根组件------{{ countFromSon }}</h1>
<button @click="showthis">打印this</button>
showthis(){
console.log(this)//VueComponent {_uid: 2, _isVue: true, __v_skip: true, _scope:
//EffectScope, $options: {…}, …}
this.$refs.myh1.style.color='red'
},
获取组件引用,把子组件中right.vue里的count的值改成0
<button @click="onreset">重置right组件中的count</button>
<Right @numchange="getNumCount" ref="comright"></Right>
onreset(){
//this.$refs.comright.count=0
this.$refs.comright.reset()
}
$nextTick(cb)会把cb回调函数放在下一个dom更新后执行。
$nextTick(()=>{this.$refs.iptref.focus()})
不可以用updated。