angular - 使用 Angular2 的预引导加载屏幕避免一秒钟的空白页面

标签 angular animation lazy-loading angular2-routing

我正在像这样在 Angular 2 中设置预引导加载动画:

<app-root>
  <div class="background">
    <div class="wrapper">
      <div class="loading-ball">
      </div>
    </div>
  </div>
</app-root>

但是当我添加延迟加载模块功能时,我开始在动画和最终页面之间看到 1 秒的空白页

可能是因为我在加载最终模块之前完成了 app-root 组件的加载。

有什么方法可以避免这种情况?

最佳答案

发生这种情况是因为您的应用程序组件从服务器加载惰性组件,因此它需要一些时间才能显示空白屏幕。 您可以通过在 app-component.html 中导入代码来避免这种情况。

<div class="background" *ngIf="loading">
    <div class="wrapper">
      <div class="loading-ball">
      </div>
    </div>
  </div>

这应该覆盖所有屏幕并在 app.component.ts 中

  constructor(
      private _router: Router
  ) { }
  loading = false;

  ngOnInit() {
      this._router.events.subscribe(v => this.navigationInterceptor(v));
  }

  navigationInterceptor(event: RouterEvent): void {
      if (event instanceof NavigationStart) {
          this.loading = true;
      }
      if (event instanceof NavigationEnd) {
          this.loading = false;
      }
      if (event instanceof NavigationCancel) {
          this.loading = false;
      }
      if (event instanceof NavigationError) {
          this.loading = false;
      }
  }

关于angular - 使用 Angular2 的预引导加载屏幕避免一秒钟的空白页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44475447/

相关文章:

javascript - AGM-Map 是否支持 Google Maps API 所做的一切?

angular - IONIC 后退按钮在任何情况下都不起作用

css - webkit 不在绝对定位元素上设置边框半径动画

javascript - 延迟加载带有图像的 HTML5 部分

angular - Ionic 2 中的 RecyclerView 等效项

angular - 在 Angular 2 中提交后如何清除表单?

swift - 在 iOS 应用程序中使用计时器和循环播放流畅的动画

swift - 将 Subview 置于最前面会弄乱 UIView.animate

NHibernate 延迟加载行为

lazy-loading - 帮助处理 ActiveRecord 异常 "Failed to lazily initialize a collection - no session"