Dik*_*sha 0 loading-image angular2-routing angular
我已经尝试过这个解决方案,但无法使其正常工作。我对 Angular2 完全陌生(不了解 AngularJS)。好吧,加载屏幕没有显示,我想我需要添加一些超时,以便它有一些时间显示,但我不知道在哪里以及如何显示。
应用程序组件.ts
import {Component} from '@angular/core';
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: ['./loadingCSS.css']
})
export class AppComponent {
// 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 {
if (event instanceof NavigationStart) {
this.loading = true;
}
if (event instanceof NavigationEnd) {
this.loading = false;
}
// Set loading state to false in both of the below events to hide the spinner in case a request fails
if (event instanceof NavigationCancel) {
this.loading = false;
}
if (event instanceof NavigationError) {
this.loading = false;
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序组件.html
<a [routerLink]="['/ArrayOP']"><button>Array operation</button></a>
<a [routerLink]="['/Calci']"><button>Calculator</button></a>
<div class="loading-overlay" *ngIf="loading">
Loading<span class="d">.</span><span class="d d-2">.</span><span class="d d-3">.</span>
</div>
<router-outlet></router-outlet>
Run Code Online (Sandbox Code Playgroud)
我想实现加载屏幕显示在整个应用程序的每个路由中,而我试图显示的加载屏幕就是这个(代码参考)。任何建议都会有很大帮助。
我找到了解决方案。如果将来有人需要的话,发布它。
这就是我所做的...我向app.component.tssetTimeout添加了功能
import {
Component
} from '@angular/core';
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: ['./loadingCSS.css']
})
export class AppComponent {
// 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 {
if (event instanceof NavigationStart) {
this.loading = true;
}
if (event instanceof NavigationEnd) {
setTimeout(() => { // here
this.loading = false;
}, 2000);
}
// Set loading state to false in both of the below events to hide the spinner in case a request fails
if (event instanceof NavigationCancel) {
setTimeout(() => { // here
this.loading = false;
}, 2000);
}
if (event instanceof NavigationError) {
setTimeout(() => { // here
this.loading = false;
}, 2000);
}
}
Run Code Online (Sandbox Code Playgroud)
并将 HTML 更改为
<div class="loading-overlay" *ngIf="loading">
Loading<span class="d">.</span><span class="d d-2">.</span><span class="d d-3">.</span>
</div>
<div *ngIf="!loading">
<router-outlet></router-outlet>
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4793 次 |
| 最近记录: |