'How add Name to Route in Angular 13 as like Vue
Right now I would like now how to set the name of a route in angular like Vue as I don't want to get the "path" but the alias, the same way as can be done in vue.
This is my app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LanguagesComponent } from './languages/languages.component';
const routes : Routes = [
{path : 'login',component:LoginComponent},
{path : 'languages',component:LanguagesComponent, name:"languages.index"}, //Set the name to route as can be done in vue
{path : 'unauthorized',component:UnauthorizedComponent}
]
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Then I want to get the route name since a service in angular. This is my sevice File:
import { LocationStrategy } from '@angular/common';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class PermissionByRoleService {
constructor(public router: Router,private location: LocationStrategy){}
get permissionByRole(): boolean {
if(this.getCurrentRouteName()=="languages.index"){
return true;
}
return false;
}
getCurrentRouteName(){
console.log(this.location.path() ); //Get '/languages'
console.log(this.router.url ); // I'm supposed to get '/languages' but I get '/'
console.log();// How get route name something Similar To Vue
return this.router.url ; // I neeed return route name according to porpierties of routes
}
}
Would it be possible to do this?
If so.
How can I do this?
I appreciate any help
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
