Home >Web Front-end >JS Tutorial >Comprehensive optimization strategy for vue projects
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)
Online generation, such as Zhitu and Youpaiyun
gulp generation, gulp-webp or gulp-imageisux
canvas Generate
2. Reduce image requests and use sprite images
to generate online: sprites Generator, Tencent’s gopng, spriteme
Code generation: gulp.spritesmith or sass compass
##PagePerformance optimization
Lazy loading of images or componentsUse vue-lazyload component or some other componentsvue-lazyload address:https://www.npmjs.com/package/vue -lazyload
Picture lazy loading: v-lazy or use v-lazy-container to contain a picture group// 引入一张图片 d8a37813200e90cb882202e6600ac9aa // 引入一组图片 e198a21d55b046d321bc2258229981dd c3a42a5ff0bd0ce8a74aabf3cd201f44 9cadc3f16046274831d7f42c6f542623 888c1accf71027863634595a3ce6aeb1 94b3e26ee717c64999d7867364b1b4a3Component lazy loading
Vue.use(VueLazyload, { lazyComponent: true }); e08d5153eab072a80cbb84375c868756 ce0e6eae7172f2af23edab08bb2e6a83 92e1bf758431b4438818079f228c09bb
Picture preloading
Quickly display picturesUsage 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 positionThree-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 specificevents (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 overlappingWhen multiple vue components are loaded and the components are rendered through server-side data, multiple components will first overlap and then separate. SituationThree options
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!