Javascript 角度2库配置
目前,我正在尝试创建我的第一个Angular 2库,一个translate管道。现在,我正试图让开发人员能够将带有翻译的对象插入到模块中,以便管道可以使用它 如何将某种配置/对象插入到库中,以便我的所有组件、管道和服务都可以使用它 我的库看起来像: 索引Javascript 角度2库配置,javascript,angular,Javascript,Angular,目前,我正在尝试创建我的第一个Angular 2库,一个translate管道。现在,我正试图让开发人员能够将带有翻译的对象插入到模块中,以便管道可以使用它 如何将某种配置/对象插入到库中,以便我的所有组件、管道和服务都可以使用它 我的库看起来像: 索引 import { NgModule, ModuleWithProviders } from '@angular/core'; import { CommonModule } from '@angular/common'; import { T
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: []
};
}
}
翻译.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;
}
}
我的测试项目:
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 { }
只需让用户将配置对象传递给
forRoot()
方法,然后将其设置为服务
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 }
]
}
}
}
用户可以这样做
imports: [
TranslateModule.forRoot({
someProp: someValue,
anotherProp: anotherValue
})
]
你好,谢谢你回答我的问题!我已经认为我必须用它来实现根函数。然而,我得到了一个错误和警告。错误:
Uncaught TypeError:\uu网页包\u导入的模块\u 0\uu角度\u核心\uuu。InjectionToken不是构造函数
。警告:在“@angular/core”中未找到导出“InjectionToken”
。你知道为什么会抛出这个错误吗?此外,我还必须为InjectionToken的构造函数添加一个描述,new InjectionToken('blablalba')
。如果您仍然使用Angular 2.x,那么应该使用OpaqueToken
<代码>注入令牌添加到Angular 4.x中。当您使用OpaqueToken
时,您应该给它一个唯一的名称,例如newopaquetoken('translate.config')
此外,您不必使用InjectionToken
。如果需要,可以将TRANSLATE\u CONFIG
设置为字符串。它仍然可以工作。我已经实现了类似的方法,它在我的本地应用程序中工作得很好,但是当我部署应用程序时,在库中配置时,我收到了null,有什么想法吗?非常好的解决方案。我在其中看到的一个问题是在forRoot
函数中的返回对象之前添加代码,这将导致在构建优化器设置为true
的应用程序时出错
imports: [
TranslateModule.forRoot({
someProp: someValue,
anotherProp: anotherValue
})
]