Vue - 何时以及为何使用 $el [英] Vue - when and why using $el

查看:56
本文介绍了Vue - 何时以及为何使用 $el的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里找到了这个答案:https://stackoverflow.com/a/50431015/11735826我想知道为什么 .$el 在这里使用,还有为什么它没有 el 元素就不能工作?

I've found this answer here: https://stackoverflow.com/a/50431015/11735826 and i wonder why .$el was used here, and also why does it not work without the el element?

推荐答案

当你在 html 标签上使用 ref 属性时,DOM 元素由 this.$refs.modal 返回.

when you use ref attribute on the html tag, the DOM-element is returned by this.$refs.modal.

当你在模板标签上使用ref属性时,返回的是组件实例,所以this.$refs.modal.$el直接返回DOM元素.参见 https://vuejs.org/v2/api/#vm-el

when you use ref attribute on the template tag, the component instance is returned, so this.$refs.modal.$el returns directly the DOM element. See https://vuejs.org/v2/api/#vm-el

这篇关于Vue - 何时以及为何使用 $el的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆