<template>
<div >
<base-input ref="usernameInput" v-model="value">
</base-input>
<button @click="focusInputInSubComponent">focus input in sub component</button>
</div>
</template>
<script>
import Vue from 'vue'
Vue.component('base-input', {
data(){
return {
value: ''
}
},
methods:{
focus: function () {
this.$refs.input.focus()
}
},
template: `
<input
ref="input"
v-bind="$attrs"
v-bind:value="value"
v-on:input="$emit('input', $event.target.value)"
>
`
})
export default{
components: {
},
data() {
return {
value: 'test'
}
},
methods:{
focusInputInSubComponent(){
this.$refs.usernameInput.focus()
}
}
}
</script>
<style >
</style>
Vue官方文档(24): 通过ref访问子组件实例或子元素
最新推荐文章于 2024-11-01 17:01:39 发布