项目作者: SlimenTN

项目描述 :
A simpel Angular pipe to tag routes by names
高级语言: TypeScript
项目地址: git://github.com/SlimenTN/routing-pipe.git
创建时间: 2019-09-04T12:30:36Z
项目社区:https://github.com/SlimenTN/routing-pipe

开源协议:

下载


routing-pipe

It’s very annoying to call routes in an angular app by it’s full name espacially when the app starts to grow because in this case changing a route will be a pure pain.
so inspired from symfony’s routing system, this pipe will make it much easier to work with routes by simply naming them.

  1. @NgModule({
  2. declarations: [RoutingPipe]// import the pipe
  3. imports: [
  4. RouterModule.forRoot([
  5. { path: 'my/full/path', component: AcmeComponent, data: { name: 'my_path_name'}},// just add the "name" attribut within the data object of the route
  6. ])
  7. ],
  8. providers: [RoutingPipe]// add it here if you want to inject it in some components
  9. })
  10. export class AppModule { }

Now after naming our route, here is how to use it:

inside some.component.html:

  1. <a [routerLink]="'my_path_name'|path">my path</a>

inside some.component.ts:

  1. export class SomeComponent implements OnInit {
  2. constructor(private _router: RoutingPipe) { }
  3. ngOnInit() {
  4. }
  5. navigateToSomePathAfterAction(){
  6. this._router.navigate('my_path_name');
  7. }
  8. }

Now I know what you’re thinking

how can we use it with dynamic data in the route ?

well good question but worry not :smile:

  1. @NgModule({
  2. declarations: [RoutingPipe]// import the pipe
  3. imports: [
  4. RouterModule.forRoot([
  5. { path: 'my/full/path/:id/acme/:action', component: AcmeComponent, data: { name: 'my_path_name'}},// just add the "name" attribut within the data object of the route
  6. ])
  7. ],
  8. providers: [RoutingPipe]// add it here if you want to inject it in some components
  9. })
  10. export class AppModule { }

inside some.component.html:

  1. <a [routerLink]="'my_path_name'|path:{id:'1', action:'show'}">my path</a>

inside some.component.ts:

  1. export class SomeComponent implements OnInit {
  2. constructor(private _router: RoutingPipe) { }
  3. ngOnInit() {
  4. }
  5. navigateToSomePathAfterAction(){
  6. // to replace params in the path
  7. this._router.navigate('my_path_name', {id:'1', action:'show'});
  8. // to add query params
  9. this._router.navigate('my_path_name', {id:'1', action:'show'}, {queryParams: {key: 'value'}});
  10. }
  11. }

or simply if you want to get the path (not navigate to it) you can call the transform() function like so:

  1. let path = this._router.transform('my_path_name', {id:'1', action:'show'});

If you have any thoughts or improvments please feel free to open an issue or pull request.