首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 2库配置

Angular 2库配置
EN

Stack Overflow用户
提问于 2017-04-28 22:11:16
回答 1查看 1.2K关注 0票数 3

目前,我正在尝试创建我的第一个Angular 2库,一个translate管道。现在,我正在尝试让开发人员能够将带有翻译的对象插入到模块中,以便管道可以使用它。

如何将某种配置/对象插入到我的库中,以便我的所有组件、管道和服务都可以使用它?

我的库如下所示:

index.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
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;
    }
}

我的测试项目:

代码语言:javascript
复制
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 { }
EN

回答 1

Stack Overflow用户

发布于 2017-04-28 22:55:19

只需让用户将一个配置对象传递给forRoot()方法,然后将其设置为服务

代码语言:javascript
复制
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 }
      ]
    }
  }
}

然后在你需要注入配置的地方,只需(在你的管道中)

代码语言:javascript
复制
import { Inject } from '@angular/core';
import { TranslateConfig, TRANSLATE_CONFIG } from './wherever';


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

用户会这样做

代码语言:javascript
复制
imports: [
  TranslateModule.forRoot({
    someProp: someValue,
    anotherProp: anotherValue
  })
]
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43682296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档