项目作者: Saeed-Pooyanfar

项目描述 :
create and use named routes for angular
高级语言: TypeScript
项目地址: git://github.com/Saeed-Pooyanfar/NamedRoutesAngular.git
创建时间: 2019-04-17T12:21:50Z
项目社区:https://github.com/Saeed-Pooyanfar/NamedRoutesAngular

开源协议:

下载


Angular named routes

As a laravel developer i like to have named routes feature in angular too!

Install compiled version

  1. npm install named-routes-angular --save

Import “NamedRoutesModule” in app.module.ts

  1. ...
  2. import { NamedRoutesModule } from 'named-routes-angular';
  3. @NgModule({
  4. imports: [
  5. ...
  6. NamedRoutesModule
  7. ],
  8. ...
  9. })
  10. export class AppModule {}
  11. ...

Define routes in app-routing.module.ts

  1. ...
  2. const routes: Routes = [
  3. {
  4. path: 'hero',
  5. data: {
  6. name: 'hero'
  7. },
  8. children: [
  9. {
  10. path: '',
  11. component: HeroesComponent,
  12. data: {
  13. name: 'index'
  14. }
  15. },
  16. {
  17. path: ':id',
  18. children: [
  19. {
  20. path: 'show',
  21. component: HeroShowComponent,
  22. data: {
  23. name: 'show'
  24. }
  25. },
  26. {
  27. path: 'edit',
  28. component: HeroEditComponent,
  29. data: {
  30. name: 'edit'
  31. }
  32. }
  33. ]
  34. }
  35. ]
  36. }
  37. ];
  38. @NgModule({
  39. imports: [RouterModule.forRoot(routes)],
  40. exports: [RouterModule]
  41. })
  42. export class AppRoutingModule {}
  43. ...

Use it in view

  1. ...
  2. <a [routerLink]="'hero.edit' | namedRoute: [hero.id]">{{ hero.name }}</a>
  3. ...