【问题标题】:Angular 6: Changing the code of an external library only works with 'ng serve' and not with 'ng build'Angular 6:更改外部库的代码仅适用于“ng serve”,不适用于“ng build”
【发布时间】:2018-08-13 15:22:25
【问题描述】:

我正在为一个项目安装一个外部库。 ng-select library by Bas van den Berg,效果很好,但需要库与 onPush 更改检测策略一起使用。我需要添加一个 ChangeDetectorRef,因此我可以使用 markForCheck()。

所以我想我会转到 ng-select.es5.js 文件并从 @angular/core 导入 ChangeDetectorRef,定义并初始化 changeDetectorRef 并在需要时使用 markForCheck(),如下所示:

import { Component, ContentChild, ElementRef, EventEmitter, HostListener, Input, NgModule, Output, ViewChild, ViewEncapsulation, forwardRef, ChangeDetectorRef } from '@angular/core';

...

var SelectComponent = /** @class */ (function () {
/**
 * @param {?} hostElement
 * @param {?} changeDetectorRef
 */
function SelectComponent(hostElement, changeDetectorRef) {
    this.hostElement = hostElement;
    this.changeDetectorRef = changeDetectorRef;
    console.log('constructor', this.changeDetectorRef, changeDetectorRef, ChangeDetectorRef);

...

SelectComponent.ctorParameters = function () {
    return [
        { type: ElementRef, },
        { type: ChangeDetectorRef, }
    ];
};

...

    SelectComponent.prototype.updateState = function (functionName) {
    console.log(functionName)
    var _this = this;
    setTimeout(function () {
        _this.changeDetectorRef.markForCheck();
    });
};

现在,当我在项目中执行“ng serve”时,我可以看到 markForCheck() 被正确执行,但如果我执行“ng build --aot”或“ng build --prod”,我会得到这个错误信息:

错误类型错误:无法读取未定义的属性“markForCheck”

所以经过一些调试后,我尝试在构造函数中记录 this.changeDetectorRef。在本地使用 'ng serve 它只会记录正确的内容,但在 'ng build' 中会记录未定义的内容。

这怎么可能? 'ng serve' 和 'ng build' 之间的编译有区别吗?我需要将此代码放在其他地方吗?

编辑:如您所见,还有一个名为 hostElement 的 ElementRef 确实有效,并且完全定义和初始化与 changeDetectorRef 相同。

Edit2:好的,我在没有 --aot 或 --prod 的情况下再次尝试,然后我没有错误。但我需要它与 --prod 一起使用。我怎样才能做到这一点?所以更好的问题是:常规的“ng build”和“ng build --prod”有什么区别?

【问题讨论】:

    标签: angular npm libraries


    【解决方案1】:

    好的,我终于找到了发生了什么。

    https://github.com/angular/angular/issues/11262#issuecomment-244472000

    显然您需要在“*.metadata.json”文件中添加您使用的库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-12
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多