问题描述
我正在尝试使用 Vuetify 的 v-text-field
autofocus
但是它只在第一次有效.关闭对话框后,它不再起作用.
I am trying to use Vuetify's v-text-field
autofocus
however it works only first time. After I close the dialog, it doesn't work anymore.
这就是我想要做的:
<v-text-field ref="focus" autofocus></v-text-field>
在谷歌搜索时我发现这是一个错误,在某些版本中得到了修复但他们有我也尝试过的临时解决方案:
While googling I found out that it was a bug that was fixed in some version but they had temporary solution which I also tried:
watch: {
dialog: (val) ->
if !val
debugger
requestAnimationFrame( =>
@$refs.focus.focus()
)
}
我做错了什么还是仍然是一个错误?设置断点我看到它在那个点停止.有人能引导我走向正确的方向吗?
Am I doing something wrong or it is still a bug? Setting breakpoint I saw that it stops at that point. Can anybody lead me to the right direction?
我唯一的区别是我使用的是 Vuex 并且对话框变量在 Vuex 商店中.对话框是 getter/setter.
The only difference I have is that I am using Vuex and the dialog variable is in Vuex store. And the dialog is getter/setter.
dialog:
get: ->
return this.$store.state.my_store.isDialogOpen
set: (value) ->
this.$store.commit('my_store/MY_MUTATION', value)
推荐答案
唯一对我有用的是 v-if="dialog"
因为 autofocus 道具只适用于初始load 这就是为什么它仅在我第一次打开对话框时可用.
The only thing that worked for me was the v-if="dialog"
because the autofocus prop will only work on initial load which is why it was available only for the first time I opened the dialog.
因此在对话框中具有自动对焦功能的有效 v-tex-field 看起来像这样:
So the working v-tex-field with autofocus in dialog would look something like this:
<v-text-field v-if="dialog" label="Label" autofocus></v-text-field>
这篇关于Vuetify 的自动对焦仅适用于第一次打开模态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!