vue.js - $el 未定义,而 $ref 在 Vuejs 3 中有一个对象
问题描述
我正在尝试获取对文本框元素的引用,但 $el 未定义,而其 $ref 具有对象。
我在这里做错了什么......我过度简化了我的代码以提出问题。谢谢!
app.component('lit-entry', {
template:
/*html*/
`
<div>
{{ message }}
<input v-model="favoriteColor" type="textbox" ref="refColor" />
</div>
`,
data() {
return {
message :"Hey there!",
favoriteColor:"Blue"
}
}
, mounted() {
debugger;
var x = this.$refs.refColor.$el;
//refColor has a value but $el is undefined?!?
}
});
解决方案
对于带有ref
的组件,ref 是组件实例,$el
property 是 DOM 元素。
对于元素,即input
ref 是 DOM 元素本身,它不应该具有$el
属性。
推荐阅读
- python - 我有这些词典。我应该使用什么命令来打印第二个中的“d”键?有个窍门
- reactjs - 如何在 React js 上删除满足特定条件的列表元素
- testing - 是否可以在 testcafe-browser-provider-saucelabs 中添加 iPad 参数“deviceOrientation”?
- c# - 如何以编程方式从动态 JObject 中删除属性
- c# - 实体框架 - SaveChanges 与事务
- php - 如何过滤 wordpress the_date() 和 get_the_date() 函数以仅显示帖子发布的年份
- .net-core - Abp中如何使用EfCore Code First将实体中的注解生成到数据库的字段描述中
- linux - 如何修复 java.sql.SQLNonTransientException:java.sql.SQLNonTransientException:null DSRA0010E:SQL 状态 = 08001,错误代码 = -1,639 错误
- macos - 是否有可能在 MacOS v < 11 上获得 Apple Developer Id
- javascript - 由于无法使用 app.listen,如何使用使用 npm create-react-app 制作的 React 应用程序实现 Express.js?