【发布时间】:2017-08-28 14:30:22
【问题描述】:
我正在使用 Angular 4,我想在路线导航之间显示一个加载屏幕(基本微调器)。 我使用this 技术来订阅正确的事件,并使用 style.visibility 来显示/隐藏我的微调器。
由于某种原因,虽然我正确检测到导航的开始和结束事件,但我的微调器不会显示。事实上,虽然可见性字段正在正确更改,但微调器仍不会显示。
我设法显示它的唯一方法是强制它始终设置为“可见”而不是“隐藏”。
附上我的代码的相关部分:
app.component.ts:
constructor (private utils: UtilsService, private router: Router) {
// Intercept all navigation events
router.events.subscribe((event: RouterEvent) => {
this.navigationInterceptor(event)
})
// Calls our initializer
utils.init();
}
/* Intercept the navigator in order to display a loading screen */
navigationInterceptor(event: RouterEvent): void {
// Starting the navigation
if (event instanceof NavigationStart) {
this.loading = true;
}
// The navigation ended
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;
}
// Get our loading container
var loading_container = document.getElementById('loader-container');
// If our loading container isn't available, this is the application first launch (ie. full refresh)
// so we need to display a full loading logo
if (loading_container) {
// This is just a redirect, not a full refresh, so just hide or show the loading screen
document.getElementById('loader-container').style.visibility = this.loading ? 'visible' : 'hidden';
}
}
app.component.html:
<div style="position: fixed; left: 220px; width: calc(100% - 220px); height: 100%; z-index: 10000; background-color: rgba(255, 255, 255, 0.6);" id = "loader-container">
<div style = "top: 30%;" class="sk-spinner sk-spinner-double-bounce">
<div class="sk-double-bounce1"></div>
<div class="sk-double-bounce2"></div>
</div>
</div>
<!-- Main view/routes wrapper-->
<router-outlet>
</router-outlet>
【问题讨论】:
标签: javascript angular navigation