Вдохновленный ответом SO - Показать экран загрузки при навигации между маршрутами в Angular 2 , я хочу показать индикатор выполнения перед загрузкой главной страницы моего приложения Angular.Но это не работает.
Я создал компонент прогресса с селектором app-progress-bar'
.Его HTML-код -
progress.component.html
<div class="loading-overlay" >
<!-- show something fancy here, here with Angular 2 Material's loading bar or circle -->
<mat-progress-bar mode="indeterminate"></mat-progress-bar>
</div>
Мой компонент приложения использует css-grid
, имеющий 3 строки и 1 столбец.HTML-код
app.component.html
<div class="body__div--background" >
<div class="css-grid-container" *ngIf="loading">
<app-progress-bar></app-progress-bar>
</div>
<div class="css-grid-container" *ngIf="!loading">
<app-nav-component></app-nav-component>
<!-- the components which target this router-outlet could be containers. Don't make this content-component a container-->
<app-content-component></app-content-component>
<!--app-content-component></app-content-component> <!--this component has router-outlet to put new componnents in it -->
<app-footer-component></app-footer-component>
</div>
</div>
CSS для размещения указанных макетов:
app.component.css
.body__div--background {
background: linear-gradient(45deg,#33b1f8 37%,#6e90f6 100%); /*syntax linear-gradient(direction, color1 limit, color2 limit)*/
color:#555555;
font-family: Helvetica;
line-height:1.5;
font-size: 11px;
letter-spacing: 0.25px;
}
.css-grid-container{
height:100vh; /*height of the container is same ahs height of the view port.*/
display: grid;
grid-gap:20px;
grid-template-columns: 1fr; /* 1 columns*/
grid-template-rows: auto 15fr 1fr; /* 3 rows. Auto takes height of navigation, remaining is divided into 2 rows, middle row is 15 times larger than the 3rd row.*/
}
app-nav-component {
grid-column: 1 / -1;
grid-row: 1 / 2;
}
app-content-component{
grid-column: 1 / -1;
grid-row: 2 / 3;
}
app-footer-component{
grid-column: 1 / -1;
grid-row: 3 / -1;
}
app-progress-bar{
grid-column: 1 / -1;
grid-row: 1 / -1;
}
и .ts
файл
app.component.ts
import { Component } from '@angular/core';
import * as moment from "moment";
import {MatProgressBarModule} from '@angular/material/progress-bar';
import {
Router,
// import as RouterEvent to avoid confusion with the DOM Event
Event as RouterEvent,
NavigationStart,
NavigationEnd,
NavigationCancel,
NavigationError
} from '@angular/router'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
// Sets initial value to true to show loading spinner on first load
loading = true
constructor(private router: Router) {
router.events.subscribe((event: RouterEvent) => {
this.navigationInterceptor(event)
})
}
// Shows and hides the loading spinner during RouterEvent changes
navigationInterceptor(event: RouterEvent): void {
console.log("received event from Router:",event)
if (event instanceof NavigationStart) {
console.log("NavigationStart")
console.log("old load"+this.loading)
this.loading = true
console.log("new load"+this.loading)
}
if (event instanceof NavigationEnd) {
console.log("NavigationEnd")
console.log("old load"+this.loading)
this.loading = false
console.log("new load"+this.loading)
}
// Set loading state to false in both of the below events to hide the spinner in case a request fails
if (event instanceof NavigationCancel) {
console.log("NavigationCancel")
console.log("old load"+this.loading)
this.loading = false
console.log("new load"+this.loading)
}
if (event instanceof NavigationError) {
console.log("NavigationError")
console.log("old load"+this.loading)
this.loading = false
console.log("new load"+this.loading)
}
}
}
Я использую вышеуказанный компонент в main.html
<app-root>Loading ...</app-root>
Все, что я вижуэто сообщение Loading ...
, и затем моя страница загружается, но я не вижу индикатор выполнения.Как я могу показать индикатор выполнения или счетчик, пока Angular загружает приложение?