Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
Configuración de la biblioteca angular 2

Actualmente estoy tratando de crear mi primera biblioteca Angular 2, una tubería de traducción. En este momento, estoy tratando de hacer que el desarrollador pueda insertar un objeto con traducciones en el módulo para que la tubería pueda usarlo.

¿Cómo puedo insertar algún tipo de configuración/objeto en mi biblioteca para que todos mis componentes, tuberías y servicios puedan usarlo?

Mi biblioteca se parece a:

índice.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: [] }; } }

traducir.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; } }

Mi proyecto de prueba:

 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 answers
Answer question

0

Simplemente deje que el usuario pase un objeto de configuración al método forRoot() , luego configúrelo como un servicio

 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 } ] } } }

Luego, donde sea que necesite inyectar la configuración, simplemente hágalo (en su tubería)

 import { Inject } from '@angular/core'; import { TranslateConfig, TRANSLATE_CONFIG } from './wherever'; constructor(@Inject(TRANSLATE_CONFIG) config: TranslateConfig) {}

El usuario haría

 imports: [ TranslateModule.forRoot({ someProp: someValue, anotherProp: anotherValue }) ]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!