ngx-route-maker
TypeScript icon, indicating that this package has built-in type declarations

1.1.5 • Public • Published

NgxRouteMaker

This library for generating routes

Install

$ npm install ngx-route-maker --save

Add package to NgModule imports:

import { NgxRouteMakerConfig, NgxRouteMakerModule } from 'ngx-route-maker';

const routesConfig: NgxRouteMakerConfig = {
  routes: {
    home: '/',
    dashboard: '/dashboard',
    pages: {
      _path: 'pages',
      page: '/{slug}',
      anotherPage: '/another/{slug}'
    }
  }
};

@NgModule({
  ...
  NgxRouteMakerModule.forRoot(routesConfig),
  ...
})

Usage

  • Usage with service:
import { NgxRouteMakerService } from 'ngx-route-maker';

export class DashboardComponent {

  constructor(
    private routeMaker: NgxRouteMakerService
  ) {
  }

  public getUrlByString(): string {
    return this.routeMaker.make('pages.page', { slug: 'new-page' });
  }

  public getUrlByArray(): string {
    return this.routeMaker.make(['pages', 'page'], { slug: 'new-page' });
  }

}

Usage with service

  • From string: /pages/new-page
this.routeMaker.make('pages.page', { slug: 'new-page' });
  • From array: /pages/new-page
this.routeMaker.make(['pages', 'page'], { slug: 'new-page' });
  • With default route for empty result: /home
this.routeMaker.make('fake.path', {}, '/home');

otherwise, you'll get an error for non-existent route

Usage with pipe:

  • From string: /pages/new-page
<a [routerLink]="'pages.page' | makeRoute: { slug: 'new-page' }">My Link</a>
  • From array: /pages/new-page
<a [routerLink]="['pages', 'page'] | makeRoute: { slug: 'new-page' }">My Link</a>
  • With default route for empty result: /home
<a [routerLink]="'pages.page' | makeRoute: { slug: 'new-page' } : '/home'">My Link</a>

NgxRouteMakerConfig

Key Value
routes NgxRouteMaker

NgxRouteMaker

Key Value
_path that's prefix which will be applied for each inside route in the same depth
[key: string] string or NgxRouteMaker

Package Sidebar

Install

npm i ngx-route-maker

Weekly Downloads

1

Version

1.1.5

License

MIT

Unpacked Size

130 kB

Total Files

35

Last publish

Collaborators

  • cach