Home Web Front-end JS Tutorial How to implement Vue page skeleton screen

How to implement Vue page skeleton screen

May 28, 2018 am 11:32 AM
accomplish page

This time I will show you how to implement the Vue page skeleton screen, and what are the precautions for implementing the Vue page skeleton screen. The following is a practical case, let's take a look.

When developing webapps, they are always affected by the long loading time of the first screen. The mainstream solution is to display the loading image effect before the loading is completed, and some large companies will configure a set of server-side rendering. architecture to solve this problem. Considering the series of problems that SSR needs to solve, more and more APPs adopt the "skeleton screen" method to improve user experience.

Xiaomi Mall:

1. Analyze the content loading process of the Vue page## The entry index.html in the #vue project has only simple content:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <title>Document</title>
</head>
<body>
  <p id="root">    
  </p>
  <script type="text/javascript" src="bundle.js"></script></body>
</body>
</html>
Copy after login

After the js is executed,

p#root

will be completely replaced with the dom rendered by vue. We added a simulated skeleton screen to

p#root

and adjusted the network speed in Chrome developer tools: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;p id=&quot;root&quot;&gt;   这里是骨架屏 &lt;/p&gt;</pre><div class="contentsignin">Copy after login</div></div>

From this It can be seen that the skeleton screen can be realized by directly inserting the skeleton screen content into

p#root

.

2. Use vue-server-renderer to implement the skeleton screenWe need the skeleton screen to be a separate

.vue

file, so we need to use vue-server-renderer. Students who know something about Vue server-side rendering must know that this plug-in can package the Vue project into a bundle on the node side, and then generate the corresponding HTML from the bundle. The first is to generate the project:

.
├── build
│  ├── webpack.config.client.js
│  └── webpack.config.server.js
├── src
│  └── views
│    ├── index
│    │  └── index.vue
│    ├── skeleton
│    │  └── skeleton.vue
│    ├── app.vue
│    ├── index.js
│    └── skeleton-entry.js
├── index.html
└── skeleton.js
└── package.json
Copy after login

Vue’s server-side rendering generally uses vue-server-renderer

to package the entire project into a bundle on the node side, and here we only need one There is html with skeleton screen, so there will be a separate skeleton screenentry fileskeleton-entry.js, and a skeleton screen packaged webpack configurationwebpack.config.server. js, and skeleton.js is used to write the bundle packaged by webpack into index.html. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">//skeleton-entry.js import Vue from 'vue' import Skeleton from './views/skeleton/skeleton.vue' export default new Vue({  components: {   Skeleton  },  template: '&lt;skeleton /&gt;' })</pre><div class="contentsignin">Copy after login</div></div> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">//webpack.config.server.js const path = require('path') const { VueLoaderPlugin } = require('vue-loader') const VueSSRServerPlugin = require('vue-server-renderer/server-plugin') module.exports = {  mode: process.env.NODE_ENV,  target: 'node',  entry: path.join(dirname, '../src/skeleton-entry.js'),  output: {   path: path.join(dirname, '../server-dist'),   filename: 'server.bundle.js',   libraryTarget: 'commonjs2'  },  module: {   rules: [    {     test: /\.vue$/,     loader: 'vue-loader'    },    {     test: /\.css$/,     use: [      'vue-style-loader',      'css-loader'     ]    }     ]  },  externals: Object.keys(require('../package.json').dependencies),  resolve: {   alias: {    'vue$': 'vue/dist/vue.esm.js'   }  },  plugins: [   new VueLoaderPlugin(),   new VueSSRServerPlugin({    filename: 'skeleton.json'   })  ] }</pre><div class="contentsignin">Copy after login</div></div>Because the webpack configuration of the skeleton screen is on the node side, it requires

target: 'node'

libraryTarget: 'commonjs2'. In VueSSSRerverPlugin, the json file name of its output is specified. When webpack is executed, a skeleton.json file will be generated in the /server-dist directory. This file records the content and style of the skeleton screen and will be provided to vue-server-renderer for use. . <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">//skeleton.js const fs = require('fs') const path = require('path') const createBundleRenderer = require('vue-server-renderer').createBundleRenderer // 读取`skeleton.json`,以`index.html`为模板写入内容 const renderer = createBundleRenderer(path.join(dirname, './server-dist/skeleton.json'), {  template: fs.readFileSync(path.join(dirname, './index.html'), 'utf-8') }) // 把上一步模板完成的内容写入(替换)`index.html` renderer.renderToString({}, (err, html) =&gt; {  fs.writeFileSync('index.html', html, 'utf-8') })</pre><div class="contentsignin">Copy after login</div></div> Note that the html file as a template needs to add the placeholder at the location where the content is written. This example is written in p#root Enter:

<p id="root">
<!--vue-ssr-outlet-->
</p>
Copy after login

Finally execute

node skeleton

to realize the skeleton screen of vue. Final

index.html

:<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;!DOCTYPE html&gt; &lt;html lang=&quot;zh-CN&quot;&gt; &lt;head&gt;   &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html;charset=UTF-8&quot;&gt;   &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge,chrome=1&quot;&gt;   &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0&quot;&gt;   &lt;title&gt;Document&lt;/title&gt; &lt;style data-vue-ssr-id=&quot;a7049cb4:0&quot;&gt; .skeleton[data-v-61761ff8] {  position: relative;  height: 100%;  overflow: hidden;  padding: 15px;  box-sizing: border-box;  background: #fff; } .skeleton-nav[data-v-61761ff8] {  height: 45px;  background: #eee;  margin-bottom: 15px; } .skeleton-swiper[data-v-61761ff8] {  height: 160px;  background: #eee;  margin-bottom: 15px; } .skeleton-tabs[data-v-61761ff8] {  list-style: none;  padding: 0;  margin: 0 -15px;  display: flex;  flex-wrap: wrap; } .skeleton-tabs-item[data-v-61761ff8] {  width: 25%;  height: 55px;  box-sizing: border-box;  text-align: center;  margin-bottom: 15px; } .skeleton-tabs-item span[data-v-61761ff8] {  display: inline-block;  width: 55px;  height: 55px;  border-radius: 55px;  background: #eee; } .skeleton-banner[data-v-61761ff8] {  height: 60px;  background: #eee;  margin-bottom: 15px; } .skeleton-productions[data-v-61761ff8] {  height: 20px;  margin-bottom: 15px;  background: #eee; } &lt;/style&gt;&lt;/head&gt; &lt;body&gt;   &lt;p id=&quot;root&quot;&gt;     &lt;p data-server-rendered=&quot;true&quot; class=&quot;skeleton page&quot; data-v-61761ff8&gt;&lt;p class=&quot;skeleton-nav&quot; data-v-61761ff8&gt;&lt;/p&gt; &lt;p class=&quot;skeleton-swiper&quot; data-v-61761ff8&gt;&lt;/p&gt; &lt;ul class=&quot;skeleton-tabs&quot; data-v-61761ff8&gt;&lt;li class=&quot;skeleton-tabs-item&quot; data-v-61761ff8&gt;&lt;span data-v-61761ff8&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=&quot;skeleton-tabs-item&quot; data-v-61761ff8&gt;&lt;span data-v-61761ff8&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=&quot;skeleton-tabs-item&quot; data-v-61761ff8&gt;&lt;span data-v-61761ff8&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=&quot;skeleton-tabs-item&quot; data-v-61761ff8&gt;&lt;span data-v-61761ff8&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=&quot;skeleton-tabs-item&quot; data-v-61761ff8&gt;&lt;span data-v-61761ff8&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=&quot;skeleton-tabs-item&quot; data-v-61761ff8&gt;&lt;span data-v-61761ff8&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=&quot;skeleton-tabs-item&quot; data-v-61761ff8&gt;&lt;span data-v-61761ff8&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=&quot;skeleton-tabs-item&quot; data-v-61761ff8&gt;&lt;span data-v-61761ff8&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p class=&quot;skeleton-banner&quot; data-v-61761ff8&gt;&lt;/p&gt; &lt;p class=&quot;skeleton-productions&quot; data-v-61761ff8&gt;&lt;/p&gt;&lt;p class=&quot;skeleton-productions&quot; data-v-61761ff8&gt;&lt;/p&gt;&lt;p class=&quot;skeleton-productions&quot; data-v-61761ff8&gt;&lt;/p&gt;&lt;p class=&quot;skeleton-productions&quot; data-v-61761ff8&gt;&lt;/p&gt;&lt;p class=&quot;skeleton-productions&quot; data-v-61761ff8&gt;&lt;/p&gt;&lt;p class=&quot;skeleton-productions&quot; data-v-61761ff8&gt;&lt;/p&gt;&lt;/p&gt;   &lt;/p&gt; &lt;/body&gt; &lt;/html&gt;</pre><div class="contentsignin">Copy after login</div></div>Look at the effect:

I believe you will read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to build an mpvue applet project


Chart.js lightweight chart library usage steps Detailed explanation

The above is the detailed content of How to implement Vue page skeleton screen. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to copy a page in Word How to copy a page in Word Feb 20, 2024 am 10:09 AM

Want to copy a page in Microsoft Word and keep the formatting intact? This is a smart idea because duplicating pages in Word can be a useful time-saving technique when you want to create multiple copies of a specific document layout or format. This guide will walk you through the step-by-step process of copying pages in Word, whether you are creating a template or copying a specific page in a document. These simple instructions are designed to help you easily recreate your page without having to start from scratch. Why copy pages in Microsoft Word? There are several reasons why copying pages in Word is very beneficial: When you have a document with a specific layout or format that you want to copy. Unlike recreating the entire page from scratch

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

How to quickly refresh a web page? How to quickly refresh a web page? Feb 18, 2024 pm 01:14 PM

Page refresh is very common in our daily network use. When we visit a web page, we sometimes encounter some problems, such as the web page not loading or displaying abnormally, etc. At this time, we usually choose to refresh the page to solve the problem, so how to refresh the page quickly? Let’s discuss the shortcut keys for page refresh. The page refresh shortcut key is a method to quickly refresh the current web page through keyboard operations. In different operating systems and browsers, the shortcut keys for page refresh may be different. Below we use the common W

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

How to implement exact division operation in Golang How to implement exact division operation in Golang Feb 20, 2024 pm 10:51 PM

Implementing exact division operations in Golang is a common need, especially in scenarios involving financial calculations or other scenarios that require high-precision calculations. Golang's built-in division operator "/" is calculated for floating point numbers, and sometimes there is a problem of precision loss. In order to solve this problem, we can use third-party libraries or custom functions to implement exact division operations. A common approach is to use the Rat type from the math/big package, which provides a representation of fractions and can be used to implement exact division operations.

See all articles