Loader.
Sometimes content on the page take time to load, because of which the user might see the content with improper design. To avoid that we could show the loader untill the content is loaded properly.
Below is our code for showing loader for 3 seconds on intial page load. This code is in the file app.vue, you can find this file on this path:. src > app > shared > components > loader folder
loader.html
<div class="loader-wrapper" [class.loderhide]="!show">
<div class="theme-loader">
<div class="loader-p"></div>
</div>
</div>
loader.scss
.loader-wrapper.loderhide {
display: none;
}
loader.ts
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-loader',
imports: [CommonModule],
templateUrl: './loader.component.html',
styleUrls: ['./loader.component.scss']
})
export class LoaderComponent {
public show: boolean = true;
constructor() {
setTimeout(() => {
this.show = false;
}, 3000);
}
}
We have added the loader div and given the animation in its scss file.
Toggle Loader
To show and hide the loader we toggle loaderhide class through show variable. Style for the loaderhide class has been define below, it toggles display of the div between none and visible. Initially we have kept show to true, but when the component is mounted we run a function which call the settimeout function. settimeout functions overwrites the value of show variable to false after 3 second.
If you want to make changes in the animation or the design of the loader, you can navigate to public>assets>scss>components>_loader.scss Here default styles and animation has been given, make changes as per your needs.
You can modify the timing of the loader by changing the time in setTimeout function. Instead of 3000ms you can set any time as per your needs.