Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  115] [ 7]  / answers: 1 / hits: 7292  / 4 Years ago, thu, april 16, 2020, 12:00:00

I am looking for ways of redirecting a page to the maintenance page in angular but i am new and am research different methods for turning on maintenance mode
i found a possible solution here: @ the approved answer
Angular JS redirect to page within module config



however i don't know how to implement it
if there someone who could explain it, i would appreciate it greatly


More From » angularjs

 Answers
5

using an authGuard will solve this problem



auth-guard.service.ts file:



import { Injectable } from '@angular/core';
import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router';
import { AuthService } from './auth.service';
import { Observable } from 'rxjs';

@Injectable()
export class AuthGuardMaintenance implements CanActivate {

constructor(
private authService: AuthService, private router: Router
) {}

canActivate(): Observable<boolean> | Promise<boolean> | boolean {
if (this.authService.inMaintenance()) {
alert('This Site Is Still Under Maintenance')
this.router.navigate(['/maintenance']);
return false;
} else {
this.router.navigate(['/']);
return true;
}
}

}


auth.service file:



import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root'
})
export class AuthService {

constructor() { }

inMaintenance() {
return false;
}
}


then import it in the app.module.ts file and add it to providers



then import the auth guard to the app-routing.module.ts file add the property



canActivate: [AuthGuardMaintenance] 


to the the root route
eg



export const routes: Routes = [
{ path: '', component: MainComponent, canActivate: [AuthGuardMaintenance] },
{ path: 'maintenance', component: MaintenanceViewComponent },
{ path: '**', component: PageNotFoundComponent },
];

[#4132] Tuesday, April 14, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cartersinceren

Total Points: 442
Total Questions: 116
Total Answers: 106

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
cartersinceren questions
;