【问题标题】:Angular @Input inheritance causing weird compilation error with ng-packagrAngular @Input 继承导致 ng-packagr 出现奇怪的编译错误
【发布时间】:2019-06-03 18:47:05
【问题描述】:

在尝试使用@Input 继承时,我在 Angular 7 中遇到了一个非常模糊的错误消息。错误消息没有任何意义,因为我有 1 个强制 @Input 和 2 个可选 @Input 所以数学不会加起来......

Directive MyComponent, Expected 2 arguments, but got 1.

    at Object.<anonymous> (/home/project/node_modules/ng-packagr/lib/ngc/compile-source-files.js:53:68)
    at Generator.next (<anonymous>)
    at /home/project/node_modules/ng-packagr/lib/ngc/compile-source-files.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (/home/project/node_modules/ng-packagr/lib/ngc/compile-source-files.js:3:12)
    at Object.compileSourceFiles (/home/project/node_modules/ng-packagr/lib/ngc/compile-source-files.js:19:12)
    at Object.<anonymous> (/home/project/node_modules/ng-packagr/lib/ng-v5/entry-point/ts/compile-ngc.transform.js:26:32)
    at Generator.next (<anonymous>)
    at /home/project/node_modules/ng-packagr/lib/ng-v5/entry-point/ts/compile-ngc.transform.js:7:71
    at new Promise (<anonymous>)

我已经创建了这个简化的测试用例 (https://stackblitz.com/edit/angular-p1r1mn)... 这代表了我正在尝试做的事情,如果我用ng-packagr 编译简化的测试用例,一切都编译得很好,没有抛出任何错误...但是在我的实际项目(也使用ng-packagr)它不起作用并抛出上述错误...

如果我从基类中删除 @Input 并将其移至子类,那么一切正常......但是使用 angular 7,@Input 继承应该不是问题。

有人可以给我一些关于如何解决此错误的指示吗? :(

添加了示例模板代码...错误肯定不是模板。如您所见,我只是将 1 个参数传递给它,但不知何故它是“期望 2”,我不知道它在哪里或如何甚至可以期望 2 个参数。我不知道调试这个的好方法。

<div>
    <ng-container *ngIf="gridConfig">
        <my-ag-grid [config]="gridConfig"></my-ag-grid>
        <my-component [config]="myPluginConfig"></my-component>
     </ng-container>
</div>

【问题讨论】:

    标签: angular angular7 ng-packagr


    【解决方案1】:

    我认为 MyComponent 使用的某些“指令”与参数签名不匹配。

    添加 MyComponent 模板代码将帮助其他人回答。 :)

    【讨论】:

    • 很确定不在模板中,我添加了一个示例模板代码。基本上与您在我的 stackblitz 示例中发现的非常相似。
    • 我看不到“指令”,我检查了 app.module.ts。但我找不到“指令”。
    • 查看您正在使用的指令。看起来您无法使用不相等的代码跟踪错误。这似乎是一个非常敏感的问题。
    • 就是这样,我什至没有使用指令......是一个组件(就像在我的堆栈闪电战中一样)。我的代码在ng serve 中也运行良好,只是ng-packagr 正在编译它。
    【解决方案2】:

    我的天啊。所以问题是因为我的组件中有以下内容:

    @HostListener('document:click', ['$event'])
    stuffClicked($event, fromComponent) {
    

    但是来自ng-packagr 的错误消息根本没有指向任何与此问题相近的东西! Directive MyComponent, Expected 2 arguments, but got 1. 实际上是想告诉我,@HostListener 传递了 1 个参数,但我的 stuffClicked 函数需要 2 个参数。这看起来像 ng-packagr 错误。

    无论如何,要绕过这个问题,只需将第二个参数设为可选fromComponent?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      相关资源
      最近更新 更多