Home> Web Front-end> Vue.js> body text

How to get dom in vue.js

coldplay.xixi
Release: 2020-11-10 11:02:57
Original
2654 people have browsed it

Vue.js method to obtain DOM: 1. Define the ref attribute in the original tag pair or sub-component in HTML, and use [this.$refs] after the [mounted(){}] method to obtain the DOM element ;2. Mounted continues to use [this.$refs] after modifying the content of the component.

How to get dom in vue.js

【Related article recommendations:vue.js

vue.js gets dom Method:

  • Define the ref attribute in the original tag pair or subcomponent in HTML, and use this.$refs after the mounted(){} method or this method. Specifically The ref value is used to obtain the DOM element. Because when using the previous hook function of mounted, the component has not been mounted on the DOM, so naturally the elements on the DOM cannot be obtained through $refs;

  • What needs to be distinguished is that printing this .$refs. The specific ref value. If it is an original tag pair, the output result is the original tag pair. If the ref attribute is in a sub-component tag, the output is the component object, not the content in the component's corresponding template;

  • this.$refs outputs a collection of tags or subcomponents that define the ref attribute contained in the current component;

  • During the component rendering process , for example, after modifying the content of the component during mounting, continue to usethis.$refs. The ref corresponding to the modified component. At this time, what is obtained is the DOM element before modification. In order to obtain the modified DOM element , you must use the this.$nextTick() method, and use this.$refs in the callback function of this method. At this time, the modified DOM element is obtained. Of course, you can get the modified DOM elements under the updated function, but in some specific scenarios you need to get the modified DOM elements in mounted;

    Vue组件中获取DOM元素的方式之$refs的使用 
Copy after login

corresponds to the annotated mounted The content in

How to get dom in vue.js

The use of this.$nextTick in mounted allows the updated DOM elements to be obtained in mounted. Let the updated DOM element get focus in this code

How to get dom in vue.js

Related free learning recommendations:javascript(Video)

The above is the detailed content of How to get dom in vue.js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!