Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show loading animation while module lazy loads?

In my main routes I have my profile module lazy loaded on /profile like so:

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

However, my profile module takes ~1.5 - 2 seconds to load which is quite slow. I'd like to show some sort of loading animation while my module loads, is there anyway to do so? I tried doing this:

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>

and inside my css, I had:

  /* 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 */

But this doesn't seem to work, Is there anyway to show some loader while angular2 modules are loaded?

like image 467
Syntactic Fructose Avatar asked Dec 26 '16 23:12

Syntactic Fructose


People also ask

How do I know if lazy loading is working?

If you're not sure if lazy loading is working correctly, you can open the Chrome DevTools and check that the images are not loaded until the scroll.

How do you load component lazily?

To lazy load the component, we will use the import() method inside an async/await function. The above function first clears the container; otherwise, on every click of the button, the new instance of GreetComponent would be added in the container.

What is lazy loading of modules?

Lazy Loaded ModulesThe commands will generate two folders called modulea and moduleb. Each folder will contain its own module. ts, routing. ts and component files. If you check your app-routing.

Is lazy loading better?

Today, lazy loading is widely used in web applications to improve application performance. It helps developers reduce loading times, optimize data usage and improve the user experience. However, overusing lazy loading can affect the application performance negatively.


1 Answers

router-outlet wont put stuff inside itself , it would put it next to it.

When it's empty :

<router-outlet></router-outlet>

So , when it's loaded , it would be :

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

So router-outlet:empty shouldn't do anything.

You can say :

  router-outlet + .loading .spinner {
      opacity: 1;
      z-index: 100;
  }  
like image 55
Milad Avatar answered Oct 22 '22 01:10

Milad