We can easily code split and lazy load a route in Angular. However when the user then clicks that lazy loaded route, it make some time to actually fetch and run the JavaScript bundle. In this lesson we'll see how we can implement a loading indicator for such lazy loaded routes.

<!-- app.component.thml -->

<router-outlet>
<span class="loader" *ngIf="loading"></span>
</router-outlet>

 

import { Component } from '@angular/core';
import { Router, RouteConfigLoadStart, RouteConfigLoadEnd, RouterEvent } from '@angular/router'; @Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
loading: boolean; constructor(router: Router) {
this.loading = false; router.events.subscribe(
(event: RouterEvent): void => {
if (event instanceof RouteConfigLoadStart) {
this.loading = true;
} else if (event instanceof RouteConfigLoadEnd) {
this.loading = false;
}
}
);
}
}

  

[Angular] Show a Loading Indicator for Lazy Routes in Angular的更多相关文章

  1. [Angular] Show a loading indicator in Angular using *ngIf/else, the as keyword and the async pipe

    The network may be unreliable and loading data may take time. Thus it is important to give the user ...

  2. Angular 1 深度解析:脏数据检查与 angular 性能优化

    TL;DR 脏检查是一种模型到视图的数据映射机制,由 $apply 或 $digest 触发. 脏检查的范围是整个页面,不受区域或组件划分影响 使用尽量简单的绑定表达式提升脏检查执行速度 尽量减少页面 ...

  3. angular源码分析:injector.js文件分析——angular中的依赖注入式如何实现的(续)

    昨天晚上写完angular源码分析:angular中jqLite的实现--你可以丢掉jQuery了,给今天定了一个题angular源码分析:injector.js文件,以及angular的加载流程,但 ...

  4. angular 子路由跳转出现Navigation triggered outside Angular zone, did you forget to call ‘ngZone.run() 的问题修复

    angular 路由功能非常强大,同时angular的路由也非常脆弱,非常容易出现错误. 那么在我们遇到异常时,首先要做的是什么? 第一步:检查代码,对比官方文档,发现代码中不一致的地方进行改正. 第 ...

  5. 【Angular专题】——(2)【译】Angular中的ForwardRef

    原文地址:https://blog.thoughtram.io/angular/2015/09/03/forward-references-in-angular-2.html 作者:Christoph ...

  6. [Angular] N things you might don't know about Angular Route

    Prevent Partail Page display: By using Resolver: @Injectable() export class MovieResolver implements ...

  7. [Angular] Fetch non-JSON data by specifying HttpClient responseType in Angular

    By default the new Angular Http client (introduced in v4.3.1) uses JSON as the data format for commu ...

  8. [Angular] Create a custom validator for reactive forms in Angular

    Also check: directive for form validation User input validation is a core part of creating proper HT ...

  9. [Angular 8] Calculate and Measure Performance budgets with the Angular CLI

    Measuring is extremely important, without numbers we don’t know about potential problems and we don’ ...

随机推荐

  1. [转帖]时间序列数据库 (TSDB)

    时间序列数据库 (TSDB) https://www.jianshu.com/p/31afb8492eff 0.3392019.01.28 10:51:33字数 5598阅读 4030 背景 2017 ...

  2. [Oracle] - 使用 DBMS_UTILITY 查看异常详情

    DBMS_UTILITY.FORMAT_ERROR_BACKTRACE说明:这是在Oracle 10g数据库引入的,DBMS_UTILITY.FORMAT_ERROR_BACKTRACE内置函数返回一 ...

  3. 第五章 模块之random 、hashlib、time /datetime

    5.2 random 返回随机生成的一个实数 random.randint() 返回随机生成的一个实数 import random #调用模块​def get_random_code(length=6 ...

  4. Android--自定义Dialog style

    <style name="dialog" parent="@android:style/Theme.Dialog"> <item name=& ...

  5. vue cli3 项目优化

    vue-cli3 Prefetch (官网内容) <link rel="prefetch"> 是一种 resource hint,用来告诉浏览器在页面加载完成后,利用空 ...

  6. 软键盘 显示隐藏 测量高度 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  7. springMVC关于异常优先级的处理

    优先级 既然在SpringMVC中有两种处理异常的方式,那么就存在一个优先级的问题: 当发生异常的时候,SpringMVC会如下处理: (1)SpringMVC会先从配置文件找异常解析器Handler ...

  8. NetCore.SignalR.Demo演示

    项目github,点击https://github.com/wangpengzong/NetCore.SignalR.Demo 1.打开服务端Server(\SignalR.Server\bin\De ...

  9. windows环境下如何搭建Consul+Ocelot

    下面的是markdown格式的文档,懒得排版了,有兴趣的话可以去github上看,有源码 Github:https://github.com/yuchengao0721/Consul-Ocelot.g ...

  10. 解决Maven 编译出的jar中没有主清单属性

    出现这个问题的原因是 pom 中没有添加主程序入口 在配置中添加如下配置 <plugin> <groupId>org.apache.maven.plugins</grou ...