Vue - 何时以及为何使用 $el

Vue - when and why using $el

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

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

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

$el returns 给定 Vue 实例(无论是主实例还是组件)绑定到的 HTML 元素。通过使用 this.$refs.modal.$el 答案获取 this.$refs.modal 的基础 HTML 元素,然后将其封装在 jQuery 对象中以调用模态方法。