Git Product home page Git Product logo

ngx-advanced-router's Introduction

ngx-advanced-router

Npm package version Npm package license Npm downloads total

GitHub latest commit

Example

Example code available here

Usage

  1. Install the package:
npm install --save ngx-advanced-router
  1. Create a service that extends AdvancedRouteService. This is where you'll supply your routes.
    While the default Angular router only allows paths to be supplied as strings, ngx-advanced-router allows paths to be supplied as either strings OR or functions.
// ...
import { AdvancedRouteService } from 'ngx-advanced-router';

@Injectable({
  providedIn: 'root',
})
export class YourAdvancedRouteService extends AdvancedRouteService {
  public readonly routesConfig = {
    somePath: {
      path: 'some-path', // Regular string path
      component: SomeComponent,
    },
    someDetailPath: {
      path: (id: string) => `some-path/${id}`, // Function-generated string path
      component: SomeDetailComponent,
    }
    // ...
    fallBack: {
      path: '**',
      redirectTo: 'some-path',
    },
  };
}
  1. Import the AdvancedRouteModule into your AppModule (or AppRoutingModule) using the forRoot() method, and pass in your advanced route service class.\
// ...
import { AdvancedRouterModule } from 'ngx-advanced-router';
import { RouterModule } from '@angular/router';
import { YourAdvancedRouteService } from './services/your-advanced-route.service';

@NgModule({
  // ...
  imports: [
    AdvancedRouterModule.forRoot(YourAdvancedRouteService),
  ],
})
export class AppModule {} // or AppRoutingModule
  1. Import the route service into any places where you need to access route paths.
// ...
import { YourAdvancedRouteService } from './services/your-advanced-route.service';
import { LazyLoadedRouteService } from './modules/lazy-loaded/services/lazy-loaded-route.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent {

  constructor(
    // ...
    protected yourAdvancedRouteService: YourAdvancedRouteService,
    private router: Router,
  ) {}

  // ...
}

You can then use it inside a function:

  navigateToSomePath(): void {
    const somePath = this.yourAdvancedRouteService.routes.somePath.path;
    this.router.navigate([somePath]);
  }

Or in a routerLink in the HTML:

  <a [routerLink]="yourAdvancedRouteService.routes.somePath.path">

License

MIT

ngx-advanced-router's People

Contributors

danevandy99 avatar

Stargazers

Suhail Shariff avatar Sang Nguyen avatar Santosh Yadav avatar Newman avatar Tomas Rimkus avatar Jose Ignacio Santa Cruz G. avatar Jorge Rojas López avatar

Watchers

James Cloos avatar  avatar

ngx-advanced-router's Issues

Lazy loaded routes

How is the library supposed to work with lazy loaded routes:

const routes: Routes = [
    {
        path: 'main-menu',
        loadChildren: () => import('./pages/main-menu/main-menu.module').then(m => m.MainMenuPageModule)
    },
    // ...
];

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.