search
HomeWeb Front-endJS TutorialComprehensive optimization strategy for vue projects

Comprehensive optimization strategy for vue projects

May 23, 2018 pm 03:03 PM
optimizationcomprehensiveStrategy

This time I will bring you a comprehensive optimization strategy for the vue project. What are the precautions for the comprehensive optimization of the vue project. The following is a practical case, let's take a look.

PicturesOptimization

1. Picture size optimization, some pictures use WebP (webp compatibility needs to be considered)

  1. Online generation, such as Zhitu and Youpaiyun

  2. gulp generation, gulp-webp or gulp-imageisux

  3. canvas Generate

2. Reduce image requests and use sprite images

  1. to generate online: sprites Generator, Tencent’s gopng, spriteme

  2. Code generation: gulp.spritesmith or sass compass

##PagePerformance optimization

Lazy loading of images or components

Use vue-lazyload component or some other components

vue-lazyload address:

https://www.npmjs.com/package/vue -lazyload

Picture lazy loading: v-lazy or use v-lazy-container to contain a picture group

// 引入一张图片 
<img  src="/static/imghwm/default1.png" data-src="//domain.com/img1.jpg" class="lazy" alt="Comprehensive optimization strategy for vue projects" > 
// 引入一组图片
<p>
 <img  src="/static/imghwm/default1.png" data-src="//domain.com/img1.jpg" class="lazy" alt="Comprehensive optimization strategy for vue projects" >
 <img  src="/static/imghwm/default1.png" data-src="//domain.com/img2.jpg" class="lazy" alt="Comprehensive optimization strategy for vue projects" >
 <img  src="/static/imghwm/default1.png" data-src="//domain.com/img3.jpg" class="lazy" alt="Comprehensive optimization strategy for vue projects" > 
</p>
Component lazy loading

Vue.use(VueLazyload, {
 lazyComponent: true
});
<lazy-component>
 <img  class="mini-cover lazy" src="/static/imghwm/default1.png" data-src="img.src" alt="Comprehensive optimization strategy for vue projects" >
</lazy-component>

Picture preloading

Quickly display pictures

Usage scenario: In a component that views pictures, when constantly flipping through the pictures on the next page, the picture will appear after getting the data from the server and then displaying the picture. In the case of slow loading, you can preload the image when displaying new data. After the image is loaded, fill the image into the corresponding position

Three-party plug-in lazy loading (loading on demand)

JS files are generally loaded synchronously, and placing them in the page will block the loading of the main JS files.

Usage scenarios: When some projects must introduce files such as jquery, introducing these files inside the component will block page rendering to a certain extent, so it is dynamically loaded through specific

events (click or pop-up window) JS files such as jquery can quickly display the main page.

Asynchronous loading of pages, how to prevent components from overlapping

When multiple vue components are loaded and the components are rendered through server-side data, multiple components will first overlap and then separate. Situation

Three options

  • #When the section displayed on the page is fixed and the height of the content is not easy to change, it can be set outside the component in advance A fixed height that displays like adding content to a frame. When the page content is not fixed, in order to reduce the problem of overlapping components during asynchronous loading, you can set other components to be displayed on the first screen when the data of a certain component is loaded, and displayed through v-show.

  • When the page is fixed as a whole, you can add a skeleton to the page to prevent the page from flickering. For specific implementation, please refer to http://www.jb51.net/article/130505.htm

  • Server-side rendering page. For some pages with fixed data and few changes, you can consider passing Server-side rendering will display the page in a short time, providing a better user experience.

Reduce the size of imported external files

When the project introduces some ElementUI content, configure the .babelrc file by introducing babel-plugin-component, so that Partial components can be introduced to reduce the size of the component.

Routing lazy loading

But when using vue-router, webpack will package all components in a js file, which will cause the file to be very large, thus It will affect the loading of the homepage. The best way is to package other routes into different js files, and then load the corresponding js files when switching routes.

resolve => require([URL], resolve), 支持性好
() => system.import(URL) , webpack2官网上已经声明将逐渐废除, 不推荐使用
() => import(URL), webpack2官网推荐使用, 属于es7范畴, 需要配合babel的syntax-dynamic-import插件使用

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

Recommended reading:

How to use vue to register components

How to use Vue three-layer nested routing

The above is the detailed content of Comprehensive optimization strategy for vue projects. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version