【问题标题】:this.$refs.name.select() function not found in vue在 vue 中找不到 this.$refs.name.select() 函数
【发布时间】:2019-08-15 01:18:39
【问题描述】:

我在我的应用程序中使用 Vue refs,以便在单击按钮时选择并复制一些文本。所以我使用喜欢

//In template
<input type="text" ref="url" value="my url"/>
<button @click="copylink">Copy Link</button>


//in methods
copylink() {
    this.$refs.url.select()
}

这是未定义的。

但使用

document.querySelector('input').select()
我可以选择它。

我的问题是所有文档方法都可能使用 $refs。你是怎么做到的?

【问题讨论】:

  • 您需要发布您的组件代码以便任何人能够帮助您
  • 您的 sn-p 有效 (proof of concept)。请添加一些详细信息以帮助我们重现您的问题或添加一个实时示例(小提琴/codepen ...)

标签: vue.js vue-component


【解决方案1】:

如果你执行 console.log(this.$refs.url) 它会给你一个数组类型的结果。您所要做的就是: this.$refs.url[0].select() 它将起作用。

告诉我。

【讨论】:

    猜你喜欢
    • 2021-03-17
    • 2018-03-17
    • 2017-09-29
    • 2021-07-05
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    相关资源
    最近更新 更多