Angular CLI编译错误;预期有1个参数,但有0个

J.P

这是我的第一个有角度的项目,我一直在使用内置服务器进行运输,偶尔进行构建以推送到服务器-现在我正在尝试--prod构建...而Im出现编译错误。

通过大量的阅读,我已经设法解决了大多数问题,并且我了解AOT的需求,但是这一错误使我感到困惑

我通过应用反复使用此代码来启动表单,

<form novalidate="novalidate"
    autocomplete="off"
    [formGroup]="form"
    (submit)="form.valid && submitForm()" >

但是在其中一些模板上

x.component.html(6,3): : Expected 1 arguments, but got 0.

第6行是:(submit)=“ form.valid && SubmitForm()”>

========注:我已将代码精简化

and included it here, so you see EXACTLY what is there...

no extrapolating or generalizing.

Builds fine in dev mode

cli v(1.6.0)

我仍然遇到相同的错误-user-form.component.html(6,4):期望有1个参数,但得到0。

所以我想我会提供完整的组件和模板。

component.ts

import { Component, OnInit, EventEmitter, Output } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, NgForm, NgControl, Validators } from '@angular/forms';
import { AbstractControl } from '@angular/forms/src/model';
import { ValidationErrors } from '@angular/forms/src/directives/validators';

import { get } from 'lodash';

@Component({
    selector: 'user-form',
    templateUrl: './user-form.component.html',
})

export class UserFormComponent implements OnInit {

    form: FormGroup;
    loading = false;
    submitAttempted = false;

    constructor(
    ) { }

    get formDisabled () {
        return this.loading === true;
    }

    get xStatus () {
        const field = this.form.get('x');
        if (!field.touched) {
            return '';
        }
        if (field.errors && field.errors.required) {
            return 'This field is required';
        }
    }


    get formModel () {
        return {
            x: this.form.get('x').value
        }
    }

    ngOnInit() {
        this.form = new FormGroup({
            x: new FormControl(
                {
                    value: 'x',
                    disabled: this.formDisabled,
                },
                [
                    Validators.required
                ]
            )
        })
    }

    resetForm () {
        this.form.get('x').markAsUntouched();
        this.submitAttempted = false;
    }

    submitForm(form: NgForm) {
        console.log( this.form );
    }

    showError (fieldName: string) {
        const field = this.form.get(fieldName);
        return field.invalid && (field.touched || this.submitAttempted);
    }

}

template.ts(user-form.component.html)

<form 
    novalidate="novalidate"
    autocomplete="off"
    [formGroup]="form"
    (ngSubmit)="submitForm()" >

    <label class="input" [class.state-error]="showError('x')">
        <input type="text" name="x" placeholder="x" formControlName="x" />
        <div *ngIf="xStatus" [innerHTML]="xStatus | safeHtml"></div>
    </label>


    <button type="submit" class="btn btn-primary" (click)="submitAttempted = true">
        <i *ngIf="loading" class='fa fa-spinner fa-spin '></i>
        {{ loading ? 'Saving' : 'Add x' }}
    </button>

</form>
格兰特·埃贡

之所以会出现此错误,是因为在表单类中,submitForm会将表单作为参数,但是在模板中,您没有传递表单。

固执己见的解决方案:不要那样做。只需使用SubmitForm()即可触发对表单的验证。没有理由将表单实例交还给自己。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular Injectable装饰器-预期有0个参数但有1个

angular 5预期有0个参数,但有1个

Angular 4错误“预期有2-3个参数,但有1个”

Ionic - Angular - Ionic-native facebook:预期有 2 个参数,但有 4 个

angular.js?body = 1:9998错误:[$ resource:badcfg]资源配置错误。预期的响应包含一个数组但有一个对象

Typescript给我一个错误:预期有0个参数,但有1个

角度可注射错误:预期有0个参数,但有1个

Angular EventEmitter错误:预期0个类型参数,但得到1个

预期有2个参数,但在Angular 2中有1个

预期有 2 个参数,但在 angular build prod 中得到 1 个参数

TypeScript错误-预期有1-2个参数,但有0或更多。TS2556

TypeScript预期有1个参数,但有2个。

在带有Angular的函数参数中使用联合类型时出现编译错误

keyof不允许使用参数(“预期0个参数,但有1个”)

Redux createStore <StoreState>产生错误:预期有4个类型参数,但有1个

预期有3个类型参数,但有1个参数,但应推断2个类型

TypeScript重载“预期有0个参数,但有2个”

输入错误:预期“--nodes”至少有 1 个有效项目,但有 0 []

Angular 2测试错误:组件具有1个<ng-content>元素,但仅提供了0个插槽

Angular 2编译错误

具有2个对象参数的Excel VBA对象子调用给出了编译错误:预期=

如何在 Angular 终端中获取所有编译错误消息?

Angular - 错误 TS2554:需要 1 个参数,但得到 0

错误TS2554:期望1个参数,但得到0。Angular6

angular2-使用angular-cli进行bootstrap-sass编译错误

错误TS2794:应该有1个参数,但有0个参数。您是否忘了在类型变量“ Promise”中包含“ void”?

带有TypeScript的Angular2:@component之后声明预期的编译器错误

Ionic4错误TS2554:预期有1个参数,但得到0

打字稿错误预期有0个参数,但在Ionic Framework中有2个参数