Skeleton screen use
Vue architecture skeleton screen Outline of ideas
Defining an abstract componentWhat is an abstract component? A component that is skipped during rendering and only performs runtime operations. export default { name: 'GmSkeleton', abstract: true // properties of abstract components} Get the slot and initialize the operation skeleton screenrender(h) { const slots = this.$slots.default || [h('')] this.$nextTick().then(() => { this.handlerPrefix(slots, this.showSpin ? this.addSkeletPrefix : this.removeSkeletPrefix) }) return slots.length > 1 ? h('div', { staticClass: this.showSpin ? 'g-spinner' : '' }, slots) : slots } Here we put the slot processing method in nextTick, because the handlerPrefix needs to get the real DOM. NextTick is used to execute all methods in the sorted update queue. Before executing render, the renderWatcher of the GMSkeleton component has been collected in the update queue. Therefore, the nextTick CallBack function can get all the real DOM in the corresponding slots after rendering. If you don't understand the principle of nextTick, please move to what you don't know about nextTick. Cycle slots operation class namehandlerComponent(slot, handler/* addSkeletPrefix | removeSkeletPrefix */, init) { const originchildren = (((slot.componentInstance || {})._vnode || {}).componentOptions || {}).children const compchildren = ((slot.componentInstance || {})._vnode || {}).children !init && handler(slot) if (compchildren) this.handlerPrefix(compchildren, handler, false) if (originchildren) this.handlerPrefix(originchildren, handler, false) }, handlerPrefix(slots, handler, init = true) { slots.forEach(slot => { var children = slot.children || (slot.componentOptions || {}).children || ((slot.componentInstance || {})._vnode || {}).children if (slot.data) { if (!slot.componentOptions) { !init && handler(slot) } else if (!this.$hoc_utils.getAbstractComponent(slot)) { ;(function(slot) { const handlerComponent = this.handlerComponent.bind(this, slot, handler, init) const insert = (slot.data.hook || {}).insert ;(slot.data.hook || {}).insert = () => { // Function refactoring, modify the original component hook, and ensure that insert is only executed once insert(slot) handlerComponent() } ;(slot.data.hook || {}).postpatch = handlerComponent }).call(this, slot) } } if (slot && slot.elm && slot.elm.nodeType === 3) { if (this.showSpin) { slot.memorizedtextContent = slot.elm.textContent slot.elm.textContent = '' } else { slot.elm.textContent = slot.memorizedtextContent || slot.elm.textContent || slot.text } } children && this.handlerPrefix(children, handler, false) }) }, Step by step analysis:
The static class name for operating vnodeaddSkeletPrefix(slot) { const rootVnode = slot.componentOptions ? (slot.componentInstance || {})._vnode || {} : slot; if (rootVnode.elm) { rootVnode.elm.classList.add(this.skeletPrefix) } else { ;(rootVnode.data || {}).staticClass += ` ${this.skeletPrefix}` } }, removeSkeletPrefix(slot) { const rootVnode = slot.componentOptions ? (slot.componentInstance || {})._vnode || {} : slot; if (rootVnode.elm) { rootVnode.elm.classList && rootVnode.elm.classList.remove(this.skeletPrefix) } else if (rootVnode.data.staticClass) { rootVnode.data.staticClass = rootVnode.data.staticClass.replace(` ${this.skeletPrefix}`, '') } } addSkeletePrefix is used to add the gm-skeleton class name, while removeSkeletonPrefix is used to delete the gm-skeleton class name How to useimport Vue from 'vue' import GMSkeleton from 'path/to/GMSkeleton' Vue.use(GMSkeleton) <gm-skeleton> <Component /> <div></div> <div><span>Front-end Martin</span></div> </gm-skeleton> Passing Values
The effect is as followsThe specific style is generated according to the style written by the developer, wrapped by gm-skeleton, as shown above. The following is a simple example Full address80 lines of code to implement Vue skeleton screen The above is the details of the example of implementing the skeleton screen with vue. For more information about implementing the skeleton screen with vue, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Detailed steps for installing the decompressed version of MySQL 5.7.20 (two methods)
>>: Build a Scala environment under Linux and write a simple Scala program
Table of contents Overview Example 1) Freeze Obje...
As early as in the CSS2 recommendations in 1998, t...
background As we all know, after we develop a Jav...
When the front-end and back-end interact, sometim...
introduction When I was learning more about datab...
query_cache_limit query_cache_limit specifies the...
1. Use floating method Effect picture: The code i...
Table of contents 1. Scene introduction 2 Code Op...
Table of contents 1. State Hook 1. Basic usage 2....
Table of contents 1. Background 2. Operation step...
Today, let’s discuss an interesting topic: How mu...
The utf8mb4 encoding is a superset of the utf8 en...
This article shares the specific code for JavaScr...
MySQL 5.7 and above versions provide direct query...
The async_hooks module is an experimental API off...