当我尝试使用@Input继承时,我在Angular 7中遇到了一个非常模糊的错误消息。这个错误消息没有任何意义,因为我有一个强制的@Input和两个可选的@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继承应该不是问题。
有人能给我一些如何解决这个错误的建议吗?:(
已添加示例模板代码...错误肯定不是模板造成的。正如你所看到的,我只给它传递了一个参数,但不知何故它是“期望2",我不知道它在哪里或者怎么会期望2个参数。我不知道一个好的方法来调试它。
<div>
<ng-container *ngIf="gridConfig">
<my-ag-grid [config]="gridConfig"></my-ag-grid>
<my-component [config]="myPluginConfig"></my-component>
</ng-container>
</div>发布于 2019-06-04 14:29:39
我认为MyComponent使用的某些“指令”与参数签名不匹配。
添加MyComponent模板代码将帮助其他人回答。:)
发布于 2019-06-05 21:31:45
哦我的天啊。所以问题是因为我的组件中有以下内容:
@HostListener('document:click', ['$event'])
stuffClicked($event, fromComponent) {但是来自HostListener的错误消息根本没有指出这个问题!Directive MyComponent, Expected 2 arguments, but got 1.实际上是想告诉我,@ ng-packagr传递的是1个参数,而我的stuffClicked函数需要2个参数。这看起来像是一个ng-packagr错误。
无论如何,绕过这个问题,只是让第二个参数成为可选的fromComponent?。
https://stackoverflow.com/questions/56432951
复制相似问题