npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-conditional-child-routes

v0.1.3

Published

Lightweight Angular library for conditionally lazy-loading child routes.

Downloads

9

Readme

ngx-conditional-child-routes

Lightweight Angular library for conditionally lazy-loading child routes.

GitHub release (latest by date) npm bundle size NPM CircleCI GitHub issues

Table Of Contents

About

A lightweight Angular library that makes it super-easy to conditionally lazy-load child routes.

Installation

npm install --save ngx-conditional-child-routes

If you are using yarn

yarn add ngx-conditional-child-routes

Getting Started

  1. Initialize NgxConditionalChildRoutes in your Angular app's main.ts file.

    import { NgxConditionalChildRoutes } from 'ngx-conditional-child-routes';
       
    // ...
       
    platformBrowserDynamic()
      .bootstrapModule(AppModule)
      .then((m) => NgxConditionalChildRoutes.init(m.injector)) /* Set the injector */
      .catch((err) => console.error(err));
  2. Create your conditional route loader Your loader should implement one of the following interfaces. Refer to sample implementation provided below.

    export interface INgxConditionalChildRoutesLoader {
      loadModule(): Observable<Promise<Type<any>>>;
    }
       
    export interface INgxConditionalChildRoutesLoaderWithData<T> {
      loadModule(data?: T): Observable<Promise<Type<any>>>;
    }
  3. Register your conditional route loader in the root module

    import { NGX_CONDITIONAL_ROUTES_LOADER } from 'ngx-conditional-child-routes';
       
    providers: [
      {
        provide: NGX_CONDITIONAL_ROUTES_LOADER,
        useExisting: CustomRoutesLoader,
      }
    ]
  4. Conditionally lazy-load your child routes

    import { NgxConditionalChildRoutes } from 'ngx-conditional-child-routes';
       
    RouterModule.forRoot([
      // ... other routes
      {
        path: 'dashboard',
        loadChildren: () => NgxConditionalChildRoutes.load(),
      },
    ])

Example: Custom Child Route Loader

Here's a sample implementation of the child route loader, with the loadModule() method returning an Observable of (lazy-loaded) module.

import { Type } from '@angular/core';
import { Observable } from "rxjs";

@Injectable({ providedIn: 'root' })
export class CustomRoutesLoader implements INgxConditionalChildRoutesLoader {
  constructor(private authService: AuthService) {}

  loadModule(): Observable<Promise<Type<any>>> {
    return this.authService.role$.pipe(
      map((role) => {
        switch (role) {
          case UserRole.user:
            return import('../modules/user/user.module').then(
              (m) => m.UserModule,
            );
          case UserRole.admin:
            return import('../modules/admin/admin.module').then(
              (m) => m.AdminModule,
            );
        }
      }),
    );
  }
}

Pass data to Child Route Loader

If you need to pass data to your conditional route loader, you need to implement INgxConditionalChildRoutesLoaderWithData<T> interface.

export enum PageType {
  Dashboard,
  Profile
}

@Injectable({ providedIn: 'root' })
export class CustomRoutesLoader implements INgxConditionalChildRoutesLoader<PageType> {
  loadModule(data: PageType): Observable<Promise<Type<any>>> {
    // ...
  }
}

And then just pass the data while loading the modules.

RouterModule.forRoot([
  // ... other routes
  {
    path: 'dashboard',
    loadChildren: () => NgxConditionalChildRoutes.load(MyEnum.Dashboard),
  },
])

Contributing

Any contributions to make this project better are much appreciated. Please follow the following guidelines before getting your hands dirty.

  • Fork the repository
  • Run yarn
  • Make your changes, and don't forget to add unit tests.
  • Run lint
    npm run lint
  • Run test
    npm run test
  • Commit/Push any changes to your repository
  • Open a pull request

License

Distributed under the MIT License. See LICENSE for more information.

Acknowledgements