Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

80
Vistas
Angular 2 Library config

Currently I am trying to create my first Angular 2 library, a translate pipe. Right now I am trying to make the developer able to insert an object with translations into the module so the pipe can use it.

How can I insert some sort of config/object into my library so all my components, pipes and services can use it?

My library looks like:

index.ts

import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';

import { TranslatePipe } from './translate.pipe';

export * from './translate.pipe';

@NgModule({
    imports: [
        CommonModule
    ],
    declarations: [
        TranslatePipe
    ],
    exports: [
        TranslatePipe
    ]
})
export class TranslateModule
{
    static forRoot(): ModuleWithProviders
    {
        return {
            ngModule: TranslateModule,
            providers: []
        };
    }
}

translate.pipe.ts

import { Injectable, PipeTransform, Pipe } from '@angular/core';

@Pipe({
    name: 'translate'
})

@Injectable()

export class TranslatePipe implements PipeTransform
{

    public translations: any = null;

    constructor ()
    {
        this.translations = {};
    }

    transform(key: string): string
    {
        let translation = key;

        if (key in this.translations) translation = this.translations[key];

        return translation;
    }
}

My testing project:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';

import { TranslateModule } from 'translate-pipe';

@NgModule({
    declarations: [
        AppComponent,
        TranslateModule
    ],
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule
    ],
    providers: [
        TranslateModule
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Just let the user pass a config object to the forRoot() method, then set it as a service

import { InjectionToken } from '@angular/core';

export const TRANSLATE_CONFIG = new InjectionToken();

export interface TranslateConfig {
  someProp?: string;
  anotherProp?: string;
}

export class TranslateModule {

  // config is optional. You can use configure default below
  static forRoot(config?: TranslateConfig) { // <========
    const conf = createConfig(config); // maybe set some defaults

    return {
      ngModule: TranslateModule,
      providers: [
        { provide: TRANSLATE_CONFIG, useValue: conf }
      ]
    }
  }
}

Then wherever you need to inject the config, just do (in your pipe)

import { Inject } from '@angular/core';
import { TranslateConfig, TRANSLATE_CONFIG } from './wherever';


constructor(@Inject(TRANSLATE_CONFIG) config: TranslateConfig) {}

The user would do

imports: [
  TranslateModule.forRoot({
    someProp: someValue,
    anotherProp: anotherValue
  })
]
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda