Home Database Mysql Tutorial Angular implements preloading delay module example sharing

Angular implements preloading delay module example sharing

Jan 24, 2018 pm 02:07 PM
angular Delay module

This article mainly introduces the example of Angular implementing preloading delay module. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

In using route lazy loading, we introduced how to use modules to split applications. When accessing this module, Angular loads this module. But it takes a little time. There will be a slight delay when the user clicks for the first time.

We can fix this problem by preloading routes. Routers can load deferred modules asynchronously while the user interacts with other parts. This gives users faster access to deferred modules.

This article will add the preloading function based on the previous example.

In the previous section, our root route was defined in main.routing.ts, and we used the root route definition in app.module.ts.

It should be noted that the Home component is loaded in advance. We will render this component after the system boots.

After Angular renders the Home component, the user can interact with the application. We can preload other modules in the background through simple configuration.

Enable preloading

We provide a preloading strategy in the forRoot function.


import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { routes } from './main.routing';
import { RouterModule } from '@angular/router';
import { PreloadAllModules } from '@angular/router';

@NgModule({
 declarations: [
  AppComponent,
  HomeComponent
 ],
 imports: [
  BrowserModule,
  RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
 ],
 providers: [],
 bootstrap: [AppComponent]
})
export class AppModule { }
Copy after login

This PreloadAllModules strategy comes from @angular/router, so we also need to import it.

Customized preloading strategy

There are two predefined strategies in the router package:

  1. No preloading

  2. Preload all modulesPreloadAllModules

Load modules after 5 seconds

However, you can define it yourself A customized strategy. It's simpler than you think. For example, you want to load the remaining modules 5 seconds after your app initializes.

You need to implement the interface PreloadingStrategy. We define a custom strategy class of CustomPreloadingStrategy.


import { Route } from '@angular/router';
import { PreloadingStrategy } from '@angular/router';
import { Observable } from 'rxjs/Rx';

export class CustomPreloadingStrategy implements PreloadingStrategy {
  preload(route: Route, fn: () => Observable<boolean>): Observable<boolean> {
    return Observable.of(true).delay(5000).flatMap((_: boolean) => fn());
  }
}
Copy after login

Then, modify app.module.ts to use this custom strategy. Note that you also need to add this class in propers. to implement dependency injection.


import { BrowserModule } from &#39;@angular/platform-browser&#39;;
import { NgModule } from &#39;@angular/core&#39;;

import { AppComponent } from &#39;./app.component&#39;;
import { HomeComponent } from &#39;./home/home.component&#39;;
import { routes } from &#39;./main.routing&#39;;
import { RouterModule } from &#39;@angular/router&#39;;
import { CustomPreloadingStrategy } from &#39;./preload&#39;;

@NgModule({
 declarations: [
  AppComponent,
  HomeComponent
 ],
 imports: [
  BrowserModule,
  RouterModule.forRoot(routes, { preloadingStrategy: CustomPreloadingStrategy })
 ],
 providers: [CustomPreloadingStrategy ],
 bootstrap: [AppComponent]
})
export class AppModule { }
Copy after login

You will see that after 5 seconds, this function module is automatically loaded.

Load the specified module

We can also define additional parameters in the route to specify which modules to preload, we use data in the route definition to provide this additional data.


import { Routes } from &#39;@angular/router&#39;;
// HomeComponent this components will be eager loaded
import { HomeComponent } from &#39;./home/home.component&#39;;

export const routes: Routes = [
  { path: &#39;&#39;, component: HomeComponent, pathMatch: &#39;full&#39; },
  { path: &#39;shop&#39;, loadChildren: &#39;./shop/shop.module#ShopModule&#39;, data: {preload: true} },
  { path: &#39;**&#39;, component: HomeComponent }
];
Copy after login

Then, we define the new loading strategy.


import { Observable } from &#39;rxjs/Rx&#39;;
import { PreloadingStrategy, Route } from &#39;@angular/router&#39;;

export class PreloadSelectedModules implements PreloadingStrategy {
  preload(route: Route, load: Function): Observable<any> {
    return route.data && route.data.preload ? load() : Observable.of(null);
  }
}
Copy after login

Finally, use this strategy in app.module.ts.


import { BrowserModule } from &#39;@angular/platform-browser&#39;;
import { NgModule } from &#39;@angular/core&#39;;

import { AppComponent } from &#39;./app.component&#39;;
import { HomeComponent } from &#39;./home/home.component&#39;;
import { routes } from &#39;./main.routing&#39;;
import { RouterModule } from &#39;@angular/router&#39;;
import { PreloadSelectedModules } from &#39;./preload.module&#39;;

@NgModule({
 declarations: [
  AppComponent,
  HomeComponent
 ],
 imports: [
  BrowserModule,
  RouterModule.forRoot(routes, { preloadingStrategy: PreloadSelectedModules })
 ],
 providers: [PreloadSelectedModules ],
 bootstrap: [AppComponent]
})
export class AppModule { }
Copy after login

At this point, you can see that the module is preloaded directly. Even if you click the link, no new request will occur.

Related recommendations:

Detailed explanation of how Laravel optimizes Model queries through preloading

Simple use of CSS to implement preloaded animation effect code explanation

JS implementation of image disordered preloading function code

The above is the detailed content of Angular implements preloading delay module example sharing. 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

Video Face Swap

Video Face Swap

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

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)

WLAN expansion module has stopped [fix] WLAN expansion module has stopped [fix] Feb 19, 2024 pm 02:18 PM

If there is a problem with the WLAN expansion module on your Windows computer, it may cause you to be disconnected from the Internet. This situation is often frustrating, but fortunately, this article provides some simple suggestions that can help you solve this problem and get your wireless connection working properly again. Fix WLAN Extensibility Module Has Stopped If the WLAN Extensibility Module has stopped working on your Windows computer, follow these suggestions to fix it: Run the Network and Internet Troubleshooter to disable and re-enable wireless network connections Restart the WLAN Autoconfiguration Service Modify Power Options Modify Advanced Power Settings Reinstall Network Adapter Driver Run Some Network Commands Now, let’s look at it in detail

WLAN extensibility module cannot start WLAN extensibility module cannot start Feb 19, 2024 pm 05:09 PM

This article details methods to resolve event ID10000, which indicates that the Wireless LAN expansion module cannot start. This error may appear in the event log of Windows 11/10 PC. The WLAN extensibility module is a component of Windows that allows independent hardware vendors (IHVs) and independent software vendors (ISVs) to provide users with customized wireless network features and functionality. It extends the capabilities of native Windows network components by adding Windows default functionality. The WLAN extensibility module is started as part of initialization when the operating system loads network components. If the Wireless LAN Expansion Module encounters a problem and cannot start, you may see an error message in the event viewer log.

How to install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

An article exploring server-side rendering (SSR) in Angular An article exploring server-side rendering (SSR) in Angular Dec 27, 2022 pm 07:24 PM

Do you know Angular Universal? It can help the website provide better SEO support!

How to use PHP and Angular for front-end development How to use PHP and Angular for front-end development May 11, 2023 pm 04:04 PM

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

A brief analysis of how to use monaco-editor in angular A brief analysis of how to use monaco-editor in angular Oct 17, 2022 pm 08:04 PM

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

Python commonly used standard libraries and third-party libraries 2-sys module Python commonly used standard libraries and third-party libraries 2-sys module Apr 10, 2023 pm 02:56 PM

1. Introduction to the sys module The os module introduced earlier is mainly for the operating system, while the sys module in this article is mainly for the Python interpreter. The sys module is a module that comes with Python. It is an interface for interacting with the Python interpreter. The sys module provides many functions and variables to deal with different parts of the Python runtime environment. 2. Commonly used methods of the sys module. You can check which methods are included in the sys module through the dir() method: import sys print(dir(sys))1.sys.argv-Get the command line parameters sys.argv is used to implement the command from outside the program. The program is passed parameters and it is able to obtain the command line parameter column

How to turn off the delay in Douyu live broadcast? -How to watch the replay of Douyu live broadcast? How to turn off the delay in Douyu live broadcast? -How to watch the replay of Douyu live broadcast? Mar 18, 2024 am 10:55 AM

How to turn off the delay in Douyu live broadcast? 1. The user first clicks to enter Douyu Live, as shown in the picture. 2. Then the user clicks &quot;Settings&quot; in the &quot;Douyu Live&quot; window, as shown in the figure. 3. Then in the &quot;Settings&quot; window, click &quot;Advanced&quot;, as shown in the figure. 4. Finally, in the &quot;Advanced&quot; window, the user can cancel the delay by turning off &quot;Low latency mode is on by default&quot;, as shown in the figure. How to watch replays of Douyu live broadcast? 1. In the first step, we first find the Douyu live broadcast software icon on the computer desktop, then right-click and select the &quot;Open&quot; option. 2. In the second step, after opening the Douyu live broadcast software, we find &quot;Follow&quot; on the left side of the page. option, click to open this option and find a host you like on the right page, click the &quot;Recording&quot; option 3. The third step, proceed

See all articles