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-mix

v1.1.1

Published

Angular schematic library for generating TypeScript mixins designed for Angular Components.

Downloads

2,720

Readme

NgMix

Angular schematic library for generating TypeScript mixins designed for Angular Components.

TypeScript mixins are then used to implement a composition pattern to share common logic across Angular components.

Table of Contents

  1. Requirements
  2. Installation
  3. Usage
    1. CLI Command
    2. Mixin Overview
    3. Usage with Component
    4. Composing with Mixins
    5. Angular Lifecycle Hooks
    6. Angular @Input and @Outputs Decorators
    7. Angular Services

Requirements

  • Angular 8 or higher

Installation

Install via npm:

npm i ng-mix

Usage

CLI Command

  • To create a mixin run the following command

    ng g ng-mix:mixin
  • You will be prompted to give your mixin a name

Mixin Overview

  • The following code will be generated (sample.mixin.ts)

    import { Injectable, OnInit } from '@angular/core';
    import { BaseInjectorConstructor } from 'ng-mix';
    
    export const SampleMixin = <Tbase extends BaseInjectorConstructor>(superClass: Tbase) => {
    
    @Injectable()
    class Sample extends superClass implements OnInit {
      
      // You can inject services from the BaseClassInjector i.e
      // myService = this.injector.get(MyService);
    
      ngOnInit(): void {
        //Call super's lifecycle method
        super.ngOnInit();
    
        //Implementation here
      }		
    }
    
    return Sample;
    }
  • All mixins will inherit BaseClassInjector

Back to top

Usage with Component

  • Extend your component class with the mixin.
import { Component, Injector } from '@angular/core';
import { SampleMixin } from './sample.mixin';
import { Base } from 'ng-mix';

@Component({
  selector: 'app-component',
  templateUrl: './app-component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent extends SampleMixin(Base) {
  
  constructor(public injector: Injector) {
    //Provides injector to the mixin(s) for access to Angular Services via DI
    super(injector);
  }
}
  • Provide ng-mix's Base class to the mixin.
  • Provide the injector to the mixin(s) by passing it into the super call in the constructor.

Back to top

Composing with Mixins

  • You can mix mixins together as shown
    const Mixins = SampleOneMixin(SampleTwoMixin(Base));
    export class AppComponent extends Mixins {
    ...
    };

Back to top

Angular Lifecycle Hooks

  • When implementing an Angular lifecycle hook method on a mixin or component using mixin(s), always call super.[ lifecycle method] when mixins are used to ensure the lifecycle methods for all mixins are invoked.

    //sample.mixin.ts
    
    export const SampleMixin = <Tbase extends BaseInjectorConstructor>(superClass: Tbase) => {
    
      @Injectable()
      class Sample extends superClass {
        ...
        ngOnInit(): void {
          super.ngOnInit(); // Make sure to call super lifecycle method!
        //Implementation here
        }		
            ...
      }
    
      return Sample;
    }
    
    //app.component.ts
    
    @Component({
      ...
    })
    export class AppComponent extends SampleMixin(Base) { 
      ...
      ngOnInit(): void {
        super.ngOnInit(); // Make sure to call super lifecycle method!
        //Implementation here
      }		
      ...
    }

Back to top

Angular @Input and @Outputs Decorators

  • If @Input/@Output decorators are used in mixins, they will need to be declared in the @Component decorator of the component.

    //sample.mixin.ts
    
    export const SampleMixin = <Tbase extends BaseInjectorConstructor>(superClass: Tbase) => {
    
      @Injectable()
      class Sample extends superClass {
        @Input sampleInput = '';
        @Output sampleOutput = new EventEmitter<any>();
          
        ...
      }
    
      return Sample;
    }
    
    //app.component.ts
    
    @Component({
      ...
      inputs: ['sampleInput'],
      outputs: ['sampleOutput']
    })
    export class AppComponent extends SampleMixin(Base) { ... }

Back to top

Angular Services

  • Services can be accessed via Angular's Injector which is available in every mixin class.
    //sample.mixin.ts
    import { SampleService } from './sample.service.ts'
    
    export const SampleMixin = <Tbase extends BaseInjectorConstructor>(superClass: Tbase) => {
    
      @Injectable()
      class Sample extends superClass {
    
        sampleService = this.injector.get(SampleService);
          
        ...
      }
    
      return Sample;
    }