【问题标题】:Angular2 RC6 Inject LOCALE_ID fails after bundling捆绑后 Angular2 RC6 Inject LOCALE_ID 失败
【发布时间】:2016-09-07 21:13:29
【问题描述】:

我正在尝试捆绑一个刚刚更新到 RC6 的 Angular2 应用程序,松散地基于 this excellent blog post.

我有一个管道,它只包装内置的 DatePipe,不同之处在于它接受一个空参数,并在它收到它的情况下做一些事情。对于 RC6,必须更新它以将 _locale 参数传递给 DatePipe。这是管道:

import {Pipe, PipeTransform, Inject, LOCALE_ID} from '@angular/core';
import {DatePipe} from '@angular/common';

@Pipe({
    name: 'myDate',
})
export class MyDatePipe implements PipeTransform {
    constructor(@Inject(LOCALE_ID) private _locale: string){}

    transform(value: any, pattern?: string): string {
        if (value === null) {
            return 'Not Available';
        }

        return new DatePipe(this._locale).transform(value, pattern);
    }
}

这种依赖注入的方法和one used by the built-in DatePipe (best I can tell).是一样的

当为开发编译并加载 systemjs 时,这工作正常。但是在与汇总捆绑后,_locale 在传递给构造函数时是null。捆绑包中的实例化如下所示:

this._pipe_myDate_0 = new MyDatePipe(this.parentInjector.get(LOCALE_ID));
// this.parentInjector.get(LOCALE_ID) == null

如果有帮助的话,我可以提供汇总配置和 tsconfigs - 但现在暂时搁置,因为它会给问题增加很多内容。现在的捆绑流程是:

ngc(es2015) -> rollup(es2015) -> tsc(es5)

应用程序的其余部分运行良好!我现在已经通过将'en-US' 直接传递给 DatePipe 来解决这个问题,但我很好奇它为什么不起作用,我想正确地做到这一点。

【问题讨论】:

    标签: angular dependency-injection rollupjs


    【解决方案1】:

    为每个语言环境编译:

    ngc --locale=en-US
    

    【讨论】:

    • 您是否认为它只是因为默认语言环境无法正确编译而无法正常工作?
    • 正确。当我在没有语言环境选项的情况下编译它时,注入的 _locale 为空!我使用最终的 2.0.0 版本
    猜你喜欢
    • 2017-08-04
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多