Vuejs automatically dropped to the bottom instance code to automatically drop to the bottom after DOM loading

This article introduces you to Vuejs to automatically drop to the bottom after the DOM load is complete, and the text has been recorded for the entire issue process, and everyone who is interested in thinking about this article.

/ …………………………….. Update ….. …………………………. /

This problem is very early, and then I want to come In fact, it is a simple question. The DOM record is automatically dropped to the bottom, just need to use

vue.nexttick (() => {}) function, because VUE is a virtual DOM, not updating Dom in real time, NECTTICK () is Register the callback after the next update of the DOM.

and then look at my problem, in fact, the problem, that is, the problem of Ajax’s asynchronous execution.

/ ………………………………….. ……. Updated ……………….. /

Work project is based on the Vuejs framework, encounter a demand: Components, request a set of data to the server via AJAX when creating, showing the list and automatically pulls down to the bottom.

Implementation: First understand the life cycle of the Vuejs component, as shown below. After the discovery map, I can’t see it … here seems that I can’t put a picture, that is, the Vuejs life cycle and hook, the reader is Baidu, everywhere.

Specific details do not introduce, look at the picture or right transfer

Know the life cycle, then pay attention to demand, naturally pay attention to three Before geneate, mounted, updated.

This period is equivalent to doing some initialization work, at this timeAX is the most appropriate. The code is as follows:

Beforoutenter (to, from, next) {NEXT (Function (VM) {if (vm.datalist.length == 0) {// here Implement Ajax VM.GetversionInfo ();}});}

The code is previously written, the BeforeRouteEnter hook used (VM: because the component may not be created yet So you can’t use this, with VM instead, the code executed by the VM will be executed after it created.), This hook will be called before the Vue-Route routing, and the demand is only the first time entry Automatically execute Ajax, therefore also determined whether Data is 0, and when you write, you should not have so much trouble if you use the BeforeCreate hook, but it is just guessing, please verify your own.
 2. MOUNTED period   This hook will be called after the template (HTML) is completed and mounted, and the first thing I think is that I automatically implements an automatic Scroll to the bottom, but it is not possible. why? Because AJAX is an asynchronous operation, Ajax is executed during the BeforeCreate period, and does not guarantee that the data has been returned when MOUNTED, so it is meaningless to make a drop-down operation. 
3 Updated period

When the component object DATA data changes, and I change the DATA in the Success callback function of Ajax, so the call should be It is very suitable. However, there have been problems after the implementation, and each automatically pulls down to the bottom can only pull down to the bottom of the DATA list, and the newly added item is still below. For example

4 (new acquisition)

each time UPDThe AJAX drop-down operation is automatically dropped at the ATED, which can only be 4, not 5. The reason is that it is because Updated is executed, he is just a Data change, and it is possible that Document does not render it, causing the newly added item, and the deflection of Vue does not provide the rendered hook.

There are many information, some information, more reliable, setting a delay, setTimeout (), but this page will have obvious an obvious beating citrus feel flashing, affecting the user experience, and My processing is:

Updated () {let count = 0; let interval = setInterval (() => {if (count> 3000) {ClearInterVal (Interval) ; this.isFirst = false;} count ++; if (! document.body.scrollHeight = document.body.clientHeight) {document.body.scrollTop = document.body.scrollHeight; if (document.body.scrollTop == document.body . ScrollHeight-document.body.clientHeight) {ClearInterval (interval); this.isfirst = false;}}} = false;}}}}, 0);}

This principle should be simple, I should understand the code.


The above is the Vuejs that the small series introduces the Vuejs to automatically drop to the bottom after DOM loading.Instance code, I hope to help everyone. If you have any questions, please leave a message, the small package will reply to everyone in time. Thank you very much for your support of Tumi Cloud Website!
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment