Good day, I have implemented an "if" condition inside the ngOnInit method to handle routing issues in my manage-actives component. If a user is on the manage-actives URL and adds a "/" followed by any text, they will be redirected to the home page.
manage-actives-crud.components.ts
public ngOnInit(): void {
this.loading = true;
this.route.params.subscribe(params => {
console.log(params);
this.mode = params['mode'];
if (this.mode !== 'create' && this.mode !== 'edit' && this.mode !== 'view') {
this.router.navigate(['home']);
}
this.id = params['id'];
});
this.initData();
}
To make this functionality reusable, I am planning to create a service. I have started working on it in the util.service file. Now, I need to figure out how to call this service from the manage-actives component instead of using the if condition directly.
util.service.ts
import { Router } from '@angular/router';
import { Injectable } from '@angular/core';
import { HttpParams } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class UtilService
{
public mode: string;
constructor(
private router: Router
) { }
public invalidUrl(url: any): void
{
if (this.mode !== 'create' && this.mode !== 'edit' && this.mode !== 'view') {
void this.router.navigate(['home' + url])
}
}
}