【问题标题】:How to focus v-textarea programatically in vuetify and typescript?如何在 vuetify 和 typescript 中以编程方式聚焦 v-textarea?
【发布时间】:2019-10-21 17:53:56
【问题描述】:

现在似乎不可能,我尝试了一些疯狂的东西,例如:

(this.refs.vtextarea as any).textarea.focus()

((this.refs.vtextarea as Vue).$el as HTMLElement).focus()

等等……

Javascript 源代码对我来说几乎无法阅读,但不得不这样做很遗憾......

这是一些基本的东西,还是我遗漏了一些明显的东西?

PS:好吧,我在层次结构中的某处看到 textarea 元素...也许我可以通过一些基本的 dom 子元素访问方式访问...但这就像编写我一生中最糟糕的代码一样。

【问题讨论】:

标签: javascript typescript vue.js vuetify.js


【解决方案1】:

我现在的解决方案是这样的:

(this.$refs.<name>.$el.childNodes[0].childNodes[0].childNodes[0].childNodes[0] as HTMLElement).focus()

但正如我在尝试这种方式之前所说的那样,它尽可能丑陋。但是.. 有效。

【讨论】:

    【解决方案2】:

    您可以使用自动对焦属性

    <v-text-field
                            autofocus
                            label="Insert Document Number*"
                            single-line
                          ></v-text-field>
    

    【讨论】:

    • 这非常适合第一次加载。
    【解决方案3】:

    我让它为我的 v-text-field 工作:

    setTimeout(() => this.$refs.YOURVTEXTFIELDREF.$refs.input.focus(), 100); 
    

    【讨论】:

      【解决方案4】:

      这是纯 JavaScript,不是 TypeScript,但这对我有用:

      <v-textarea ref="entry"></v-textarea>
      
      
      <script>
      export default {
          mounted() {
              this.$refs["entry"].focus();
          }
      }
      </script>
      

      【讨论】:

        【解决方案5】:

        我让我来处理这个:

        mounted() {
            this.$nextTick(() => {
              setTimeout(() => {
                this.$refs.descriptionDescription.$refs.input.focus()
              })
            })
          },
        

        @Thomas 和 @Steven Spungin 的代码组合

        【讨论】:

        • 这是唯一对我有用的解决方案。
        【解决方案6】:

        为我工作

        setTimeout(() => {
           this.$refs.textNote.$refs.input.focus()
        })
        

        【讨论】:

          【解决方案7】:

          Vuetify 在聚焦输入时并不总是有效,即使是 $nextTick

          这就是我们对inputtextarea 的一般做法。我们实际上使用此代码并将ref 设置为我们的form,以便聚焦第一个可见的textareainput。但是,您可以只定位适合您需求的小部件。

          mounted() {
            this.$nextTick(() => {
                    const theElement = this.$refs.myRef         
                    const input = theElement.querySelector('input:not([type=hidden]),textarea:not([type=hidden])')
                    if (input) {
                      setTimeout(() => {
                        input.focus()
                      }, 0)
                    }
            });
          }
          

          $nextTicksetTimeout 的延迟可以忽略不计,通常是必需的,并且会一次又一次地为您节省时间。

          您也需要排除type=hidden,但这不会有什么坏处。

          如果ref 不是HTMLElement,而是VueComponent,您可能需要使用this.$refs.myRef.$el 来获取DOM 元素。

          【讨论】:

            【解决方案8】:

            在新答案中编辑:https://codepen.io/cby016/pen/eQXoBo

            尝试在引用后添加 $el。像这样:

            this.$nextTick(() =&gt; this.$refs.cancelTestDialog.$el.focus())

            还要确保 'ref' 被定义为组件的属性。

            Vue refs 通过在组件上添加 ref="&lt;customName&gt;" 属性来工作,您可以使用 this.$refs.. 选择它。

            OLD:在绘制 textarea 之前,您需要等待一个滴答声。你可以 使用 setTimeout 之类的技巧或使用这个 Vue 函数:

            this.$nextTick(() => { this.$refs.<name>.focus() })

            Nexttick 基本上和 setTimeout 做的一样

            【讨论】:

            • 遗憾的是,这里不是关于下一个刻度,我在它已经渲染并准备好时调用它。
            【解决方案9】:
            <template>
               <v-text-area
                  ref="myTextArea"
               />
            </template>
            
            <script>
               ...
               created () {
                  this.$refs["myTextArea"].$refs.textarea.focus()
               }
            </script>
            

            我对文本字段这样做了。我认为它应该以同样的方式工作。

            【讨论】:

            • 对我来说 this.$refs["myTextArea"].$refs.input.focus(); 工作。
            猜你喜欢
            • 1970-01-01
            • 2022-12-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-23
            • 1970-01-01
            • 2021-04-03
            • 2021-03-15
            相关资源
            最近更新 更多