AOT - Angular 6 - 指令SomeComponent,预期0个参数,但得到1.对于自制组件

Yan*_*git 17 webpack angular angular-aot ngtools

当我想在AOT中使用以下包版本编译我当前的项目时,我遇到了问题:

  • @ ngtools/@的WebPack 6.0.3
  • @ angular @ latest(6.0.2)
  • Webpack@4.0.0

我的webpack和tsconfig.json配置可以在这里找到

我面临一些与模板上使用的private/ protectedscope 有关的问题,并且一些提取参数给了一些并不真正需要它的函数(Exemple $ event,它们不用于EventBinding).

现在我有以下列表,我找不到我的问题:

/path/to/app/header/main-header/main-header.component.html(85,7)::指令TableOfContentComponent,期望0参数,但得到1.(1,1)::指令TableOfContentComponent,预期0争论,但得到1.

我的main-header.component.html文件包含:// main-header.component.html

// main-header.component.ts
@ViewChildren('headerItems') public headerItems: QueryList<HeaderItemAbstract>;
mainMenuStates = {
    hamburger: false,
    bookmarks: false,
    search: false,
    toc: false,
    medias: false,
    article: false,
    language: false    
};
Run Code Online (Sandbox Code Playgroud)

而且我TableOfContentComponent不包含任何@Input财产.

@Component({
    selector: 'ps-table-of-content-template',
    templateUrl: './table-of-content.component.html',
    animations: [slideUpAndDownAnimation]
})
export class TableOfContentComponent extends HeaderItemAbstract implements OnInit {

    toc: TableOfContentModel[];

    devices: DevicesModel;

    tocContentHeight: number;
    tocContentMargin: number;
    menuHeight: string;


    constructor(private tableOfContentService: TableOfContentService,
                private deviceService: DeviceService,
                private elemRef: ElementRef) {
        super();
        this.toc = this.tableOfContentService.tableOfContent;
    }
}
Run Code Online (Sandbox Code Playgroud)

/path/to/app/header/main-header/hamburger-menu/hamburger-menu.component.html(125,5)::指令SliderComponent,预期0参数,但得到1.(1,1)::指令SliderComponent,预期0个参数,但得到1.

hamburger-menu.component.html接近上面提到的代码:

<ps-slider-component [template]="slidable" [countItems]="associatedDocuments.length">
    <ng-template #slidable>
        <ul class="clearfix">
            <li class="ps-hmt-associated-item-wrapper pull-left slider-item"
                *ngFor="let document of associatedDocuments">
                <a href="{{ document.link }}" target="_blank" class="btn-nostyle">
                    <div class="ps-hmt-image">
                        <img src="{{ document.images.thumbnail }}" alt="">
                    </div>
                    <p class="ps-hmt-title slider-text"
                        [matTooltip]="isArticleView ? null : document.title"
                        [matTooltipPosition]="'above'"
                        [matTooltipClass]="['ps-mat-tooltip', 'ps-mat-tooltip-doc']"
                    >
                        {{ document.title }}
                    </p>
                </a>
            </li>
        </ul>
    </ng-template>
</ps-slider-component>
// On ts side
associatedDocuments: Array<AssociatedDocumentModel>;
@ViewChild('slidable') slidable: ElementRef;
Run Code Online (Sandbox Code Playgroud)

SliderComponent看起来像:

export class SliderComponent extends UnsubscribeHelper implements OnInit, OnChanges {

    @Input() template: ElementRef;
    @Input() countItems: number;
    @Input() resetSlide ?: null;
    @Input() fixedHeight?: null;
    @Input() isVariableWidth?: null;
    @Input() isBookmarks?: null;
    @Input() hasSkeleton?: boolean = false;
Run Code Online (Sandbox Code Playgroud)

/path/to/app/header/main-header/medias/dialogs/image-dialog.component.html(34,5)::指令CarouselComponent,预期0个参数,但得到1.(1,1)::指令CarouselComponent,预期0个参数,但得到1.

真的接近前一个,我认为问题是一样的.

/path/to/app/document/page/page.component.html(7,9)::指令InfinityPageScrollComponent,期望0参数,但得到1.(1,1)::指令InfinityPageScrollComponent,预期0参数,但得到1.

这里我们没有任何输入InfinityPageScrollComponent,标签就像这样调用<ps-infinity-page-scroll></ps-infinity-page-scroll>

我确切地说,当我在我的webpack上禁用AOT时,一切都像魅力一样.

我试图在没有任何结果的情况下找到AoT Do和Don'ts的解决方案.

我还注意到,如果我禁用fullTemplateTypeCheck我面临大约18 000个错误,其中一些隐式的任何类型和更奇怪的,未定义的属性为我在构造函数上声明的服务.

---编辑1:为抽象类提供代码:UnsubscribeHelper---

export abstract class HeaderItemAbstract extends UnsubscribeHelper implements AfterViewInit {
    public toggleItem: string = 'out';
    public ANIMATION_DURATION = slideUpAndDownAnimationDuration;

    constructor() {
        super();
    }

    // [Some other method]
    /**
     * Self animate after loading on DOM
     */
    ngAfterViewInit()
    {
        // Wait next to to avoid error :
        // ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked
        setTimeout(() => {
            this.toggleAnimation();
        },100);
    }
}
Run Code Online (Sandbox Code Playgroud)

抽象类代码UnsubscribeHelper:

export abstract class UnsubscribeHelper implements OnDestroy {

    subscriptions: Subscription[] = [];

    ngOnDestroy() {
        this.subscriptions.forEach(sub => sub.unsubscribe());
    }

    addSubscription(subscription: Subscription) {
        this.subscriptions.push(subscription);
    }
}
Run Code Online (Sandbox Code Playgroud)

Yan*_*git 77

好吧,我在这里准备了一个最小,完整,可验证的例子

我注意到了一个缺少的参数 @HostListner

问题样本如下:

@HostListener('window:resize', ['$event'])
onResize(): void {

}
Run Code Online (Sandbox Code Playgroud)

只需删除'$event'它就可以了.

感谢@trichetriche的帮助.

  • 这为我节省了很多时间。这太奇怪了,太不准确了,编译器没有告诉我问题实际上是什么时候。再次感谢 (4认同)
  • 我花了 2 个小时才通过正确的 Google 搜索找到了这个。我真的不明白哪个争论是不符合预期的!谢谢!!圣诞快乐! (2认同)
  • 那么 $event 将如何工作?删除 $event 将构建,但在我们需要窗口对象等的情况下不起作用。因此,解决方案可以是您可以在调用函数中添加事件参数,即使您不需要使用它。 (2认同)
  • 注意:如果您在“@Component”装饰器选项的“host”输入中定义了侦听器,当然也会发生此错误:“@Component({host: {'click': 'handleClick($event)'}” })` (2认同)

Kur*_*gor 36

我遇到了一些类似的错误 ERROR in (1,1): : Directive SomeComponent, Expected 0 arguments, but got 1. ,如本评论/sf/answers/3528666851/ 中所述,但现在发生了window:scroll

@HostListener('window:scroll', ['$event']) public onScroll(): void {
  ...
}
Run Code Online (Sandbox Code Playgroud)

这不是很明显,因为在组件内部定义的指令 (@HostListener) 就像消息中的匿名指令一样,并且不太清楚我必须在哪里搜索它。我解决这个消息的逻辑:如果我们提供$事件函数onScroll叫-我们在这里需要设置的参数event一样onScroll(event),所以里面有HostListener指令功能的处理程序没有参数,我们收到此错误。但它发生在我的案例中的第 1 行,如错误消息中所述,但 @HostListener 被声明在所有函数下方,并且可能通过提升和优化,它出现在第 1 行。

解决的代码:

@HostListener('window:scroll', ['$event']) public onScroll(event /*provide $event to method here */): void {
  ...
}
Run Code Online (Sandbox Code Playgroud)

  • 伟大的。它有效,但我刚刚从 HostListner 中删除了 $event 。这是工作 (4认同)
  • @HostListener 是一个指令。它的方法没有参数,但隐含地需要它。将(事件)参数设置为方法并解决问题:) (2认同)