How do I show a loading screen when I change a route in Angular 2?
相关问题
- Views base64 encoded blob in HTML with PHP
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
You could also use this existing solution. The demo is here. It looks like youtube loading bar. I just found it and added it to my own project.
Why not just using simple css :
And in your styles :
Or even we can do this for the first answer:
And then simply just
The trick here is, the new routes and components will always appear after router-outlet , so with a simple css selector we can show and hide the loading.
The current Angular Router provides Navigation Events. You can subscribe to these and make UI changes accordingly. Remember to count in other Events such as
NavigationCancel
andNavigationError
to stop your spinner in case router transitions fail.app.component.ts - your root component
app.component.html - your root view
Performance Improved Answer: If you care about performance there is a better method, it is slightly more tedious to implement but the performance improvement will be worth the extra work. Instead of using
*ngIf
to conditionally show the spinner, we could leverage Angular'sNgZone
andRenderer
to switch on / off the spinner which will bypass Angular's change detection when we change the spinner's state. I found this to make the animation smoother compared to using*ngIf
or anasync
pipe.This is similar to my previous answer with some tweaks:
app.component.ts - your root component
app.component.html - your root view
If you have special logic required for the first route only you can do the following:
AppComponent
I had a need for this to hide my page footer, which was otherwise appearing at the top of the page. Also if you only want a loader for the first page you can use this.
UPDATE:3 Now that I have upgraded to new Router, @borislemke's approach will not work if you use
CanDeactivate
guard. I'm degrading to my old method,ie:
this answerUPDATE2: Router events in new-router look promising and the answer by @borislemke seems to cover the main aspect of spinner implementation, I havent't tested it but I recommend it.
UPDATE1: I wrote this answer in the era of
Old-Router
, when there used to be only one eventroute-changed
notified viarouter.subscribe()
. I also felt overload of the below approach and tried to do it using onlyrouter.subscribe()
, and it backfired because there was no way to detectcanceled navigation
. So I had to revert back to lengthy approach(double work).If you know your way around in Angular2, this is what you'll need
Boot.ts
Root Component- (MyApp)
Spinner-Component (will subscribe to Spinner-service to change the value of active accordingly)
Spinner-Service (bootstrap this service)
Define an observable to be subscribed by spinner-component to change the status on change, and function to know and set the spinner active/inactive.
All Other Routes' Components
(sample):