【问题标题】:Show loading animation while module lazy loads?在模块延迟加载时显示加载动画?
【发布时间】:2017-05-15 17:29:28
【问题描述】:

在我的主要路线中,我的个人资料模块延迟加载到 /profile 上,如下所示:

  {
      path: '',
      component: HomeComponent
  },
  {
      path: 'profile', loadChildren: './profile/profile.module#ProfileModule'
  },
  {
      path: '**',
      redirectTo: '',
      pathMatch: 'full'
  }

但是,我的配置文件模块需要大约 1.5 - 2 秒才能加载,这非常慢。我想在我的模块加载时显示某种加载动画,有什么办法吗?我试过这样做:

app.component.html

<!-- other stuff ... -->

<router-outlet></router-outlet>
<div class="loading">
  <div class="spinner">
    <div class="rect1"></div>
    <div class="rect2"></div>
    <div class="rect3"></div>
    <div class="rect4"></div>
    <div class="rect5"></div>
  </div>
</div>

在我的 CSS 中,我有:

  /* default .loading styles, .loading should be invisible, opacity: 0, z-index: -1 */
  .loading {
      opacity: 0;
      transition: opacity .8s ease-in-out;
      position: fixed;
      height: 100%;
      width: 100%;
      top: 0;
      left: 0;
      background: #1B1B1B;
      color: white;
      z-index: -1;
  }
  /* .loading screen is visible when app is not bootstraped yet, .my-app is empty */
  router-outlet:empty + .loading,
  router-outlet:empty + .spinner {
      opacity: 1;
      z-index: 100;
  }  

  /* spinner animation css stuff */

但这似乎不起作用,是否在加载 angular2 模块时显示一些加载器?

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    router-outlet 不会把东西放在里面,它会放在它旁边。

    当它为空时:

    <router-outlet></router-outlet>
    

    所以,当它被加载时,它会是:

    <router-outlet></router-outlet>
    <your-code></your-code> // which is loaded afterward
    

    所以 router-outlet:empty 不应该做任何事情。

    你可以说:

      router-outlet + .loading .spinner {
          opacity: 1;
          z-index: 100;
      }  
    

    【讨论】:

    • 欣赏答案,但是我无法使用 .loader.spinner 进行任何类型的加载
    • @SyntacticFructose,我更新了我的答案,但一般来说,你应该首先确保你的动画正常工作,然后尝试将它与路由器的东西集成
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 2017-12-06
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 2017-02-22
    相关资源
    最近更新 更多