'How do I make the correct call from services using Angular?
Below, I have created a service of name util.service
to call in manage-actives-crud.component.ts
inside ngOnInit
.
Saying that everything that is different from the modes 'create', 'edit' and 'view' should redirect the user to home. This solved the problem for when the user tries to write anything at the end of the url. But when I go to click the create button my page is also being redirected to home. Can someone please help me to identify what was declared wrong on line 25?
Button "new" with bug
file 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])
}
}
}
file manage-actives-crud.component.ts
import { ActivatedRoute, Router } from '@angular/router';
import { UtilService } from 'src/app/shared/services/util/util.service';
@Component({
selector: 'app-packages-manage-actives-crud',
templateUrl: './manage-actives-crud.component.html',
styleUrls: ['./manage-actives-crud.component.scss']
})
export class managePackagesCrudComponent implements OnInit {
public loading: boolean;
public mode: string = 'create';
public id: string;
constructor(
private readonly route: ActivatedRoute,
private readonly router: Router,
private readonly util: UtilService
) { }
public ngOnInit(): void {
this.loading = true;
this.route.params.subscribe(params => {
this.mode = params['mode'];
this.id = params['id'];
this.util.invalidUrl(this.router.url);
});
this.initData();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|