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

@ng-vibe/service-locator

v1.0.0

Published

A library for Angular 17+ that implements the Service Locator pattern to manage service instances in Angular, enabling easy access and dynamic service creation without relying on Angular's built-in DI annotations.

Downloads

6

Readme

available on npmjs

The @ng-vibe/service-locator library addresses the limitations of Angular's dependency injection when it comes to non-service classes. It allows for smooth use of Angular’s DI system across all types of classes by implementing the Service Locator pattern.

Features

  • Static Access to Services: Easily retrieve instances of any registered service from anywhere in your application.
  • Dynamic Service Creation: Support for creating new service instances on-the-fly, scoped within their own injectors.
  • Simplified Service Management: Encapsulates Angular's Injector complexity, making service management straightforward and maintainable.

Why

While this approach deviates from the typical Angular DI pattern, it provides a practical solution for extending dependency injection capabilities to any class within your Angular application. It's especially useful in large applications where you need to maintain clean architecture without excessively coupling your classes to Angular's components or services structure.

How Angular's Dependency Injection Falls Short for Non-Service Classes

In Angular, dependency injection is primarily designed for services and components. However, custom classes that aren't explicitly services or components face limitations as Angular's DI can't directly inject dependencies into them. This limitation is effectively addressed by the @ng-vibe/service-locator library, which enables the use of Angular's robust DI system for all class types.

Setting Up the Angular Service Locator

The Service Locator acts as a central hub for accessing Angular’s injector, simplifying the management of both singleton services and new service instances:

  1. Initialize the Service Locator in your application module to ensure the Angular Injector is available from the start.
  2. Access Singleton Services smoothly or Create New Service Instances as needed, independent of Angular's built-in DI annotations.A

Getting Started

Installation

  1. Install @ng-vibe/service-locator locally:

    npm install @ng-vibe/service-locator

Usage

To utilize the service locator in your Angular app, import and setup the service locator in your module:

import { provideNgVibeServiceLocator } from '@ng-vibe/service-locator';

@NgModule({
  ...
  providers: [
    ...,
    provideNgVibeServiceLocator(),
  ],
  ...
})
export class AppModule {}

Example Usage

Here's how you can use the service locator to manage service instances:

import { ServiceLocator } from '@ng-vibe/service-locator';
import { LoggerDummyService } from '../services/logger-dummy.service';

export class CustomClass {
  
  constructor() {
    const loggerSingleton = ServiceLocator.getInstance(LoggerDummyService);
    const loggerNewInstance = ServiceLocator.createService(LoggerDummyService);
  }
}

Configuration Options

There are no explicit configuration options needed to use @ng-vibe/service-locator as it utilizes Angular's existing dependency injection framework.

Contributing ❤️

We welcome contributions to make @ng-vibe/service-locator even better! Whether you're fixing bugs, adding new features, or improving documentation, your help is greatly appreciated. 🌟 Check out our contribution guidelines for more information.

License

This project is licensed under the MIT License. See the LICENSE file for details.