Javascript 如何获取当前 Vue 组件的 DOM 元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36557841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:13:33  来源:igfitidea点击:

How to get DOM element of current Vue component?

javascriptjquerydomcomponentsvue.js

提问by Kamil Kie?czewski

When I'm inside some method in vue commponent and want to use some dom manipulation (by jquery) I need to get DOM element coresponnding to my component (especially in case when I have many instances of my component on page - I need to 'work' separately on each of them - so I cannot detect DOM element using only jquery selector without tricks...)

当我在 vue 组件中的某个方法中并想要使用一些 dom 操作(通过 jquery)时,我需要让 DOM 元素与我的组件关联(尤其是在页面上有很多组件实例的情况下 - 我需要' '分别对它们中的每一个进行工作 - 所以我无法仅使用 jquery 选择器来检测 DOM 元素而没有技巧......)

回答by Kamil Kie?czewski

Just by this (in any method in "methods"):

就这样(在“方法”中的任何方法中):

let domElement = this.$el;

:)

:)

But remember this is valid as long as your component is not a Fragment Instancei.e. it has a single root HTML tag

但请记住,只要您的组件不是Fragment 实例,即它具有单个根 HTML 标记,这就是有效的