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
Everyone has played the pinball and brick-breakin...
MySQL error: Error code: 1293 Incorrect table def...
Table of contents Manual backup Timer backup Manu...
1. TCP Wrappers Overview TCP Wrappers "wraps...
1|0 Compile the kernel (1) Run the uname -r comma...
Web Application Class 1. DownForEveryoneOrJustMe ...
Table of contents 1. Solution 1 (UDF) Demo Case 2...
Reminder: Whether it is planning, designing, or de...
Often, we may need to export local database data ...
Preface I encountered a situation at work: In the...
nvm nvm is responsible for managing multiple vers...
Achieve results step 1. Initial index.html To bui...
<br />We have always emphasized semantics in...
This article example shares the specific code of ...
Table of contents Stabilization Throttling: Anti-...