Home Web Front-end JS Tutorial Angular6.0 implements component lazy loading function (with code)

Angular6.0 implements component lazy loading function (with code)

Apr 28, 2018 pm 02:00 PM
Function load

This time I will bring you the lazy loading function of components in angular6.0 (with code). What are the precautions for implementing the lazy loading function of components in angular6.0. The following is a practical case. Let’s take a look. one time.

We often encounter such a problem. When we use a third-party control library, we only use one or a few of its components, which will bring along a lot of useless things, causing the volume. Too bloated. Or the home page uses many components and the loading speed of the home page is slow. At this time, we may need to load the components used within the user's visual range. As the user scrolls down, we will load these components again and load them progressively. Progressive experience, at this time you may use the functions implemented by this tool. Or some unimportant areas of a page, such as third-party advertisements or unimportant elements, can use lazy loading and lazy rendering to reduce the user's first screen waiting time. Everything happens without the user knowing it. Greatly improves user experience, especially for medium and large projects, optimization is a must!

Project addressgithub

Installation

yarn add iwe7-lazy-load
Copy after login

Use

import { Iwe7LazyLoadModule, LazyComponentsInterface } from 'iwe7-lazy-load';
// 用到的懒加载组件
let lazyComponentsModule: LazyComponentsInterface[] = [
 {
 // 组件的selector
 path: 'lazy-test',
 // 组件的相对地址
 loadChildren: './lazy-test/lazy-test.module#LazyTestModule'
 }
];
@NgModule({
 imports: [Iwe7LazyLoadModule.forRoot(lazyComponentsModule)],
 // 注意加上这些
 schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA]
})
export class AppModule {}
Copy after login
<p #ele>
 <lazy-test></lazy-test>
</p>
Copy after login
import { LazyLoaderService } from 'iwe7-lazy-load';
@ViewChild('ele') ele: ElementRef;
constructor(
 public lazyLoader: LazyLoaderService,
 public view: ViewContainerRef
) {}
ngOnInit() {
 // 开始渲染懒组件
 this.lazyLoader.init(this.ele.nativeElement, this.view);
}
Copy after login

Define lazy loading component demo

import { LazyComponentModuleBase } from 'iwe7-lazy-load';
@Component({
 selector: 'lazy-test',
 template: ` i am a lazy`
})
export class LazyTestComponent {}
@NgModule({
 imports: [
 RouterModule.forChild([{
  path: '',
  component: LazyTestComponent
 }])
 ],
 declarations: [LazyTestComponent]
})
export class LazyTestModule extends LazyComponentModuleBase {
 getComponentByName(key: string): Type<any> {
 return LazyTestComponent;
 }
}
Copy after login

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:

jQuery encoding conversion base64 upload through AJAX

vue component writing specification

The above is the detailed content of Angular6.0 implements component lazy loading function (with code). 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 Article Tags

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)

What functions does Doubao app have? What functions does Doubao app have? Mar 01, 2024 pm 10:04 PM

What functions does Doubao app have?

The difference between vivox100s and x100: performance comparison and function analysis The difference between vivox100s and x100: performance comparison and function analysis Mar 23, 2024 pm 10:27 PM

The difference between vivox100s and x100: performance comparison and function analysis

Stremio subtitles not working; error loading subtitles Stremio subtitles not working; error loading subtitles Feb 24, 2024 am 09:50 AM

Stremio subtitles not working; error loading subtitles

What is Discuz? Definition and function introduction of Discuz What is Discuz? Definition and function introduction of Discuz Mar 03, 2024 am 10:33 AM

What is Discuz? Definition and function introduction of Discuz

Comparative analysis of the functions and performance of JPA and MyBatis Comparative analysis of the functions and performance of JPA and MyBatis Feb 19, 2024 pm 05:43 PM

Comparative analysis of the functions and performance of JPA and MyBatis

What exactly is self-media? What are its main features and functions? What exactly is self-media? What are its main features and functions? Mar 21, 2024 pm 08:21 PM

What exactly is self-media? What are its main features and functions?

PHP Tips: Quickly Implement Return to Previous Page Function PHP Tips: Quickly Implement Return to Previous Page Function Mar 09, 2024 am 08:21 AM

PHP Tips: Quickly Implement Return to Previous Page Function

What are the functions of Xiaohongshu account management software? How to operate a Xiaohongshu account? What are the functions of Xiaohongshu account management software? How to operate a Xiaohongshu account? Mar 21, 2024 pm 04:16 PM

What are the functions of Xiaohongshu account management software? How to operate a Xiaohongshu account?

See all articles